PHP / mysqli does not echo single zero - php

This is a strange bug I've come across when switching my code over from 000webhost to ipage. It's the same code and a consistently different result.
The following example does nothing.
echo 0;
This example gives us '00'
echo 0;
echo 0;

Seems to be a problem with ipage's host. This is my current workaround. This workaround echoes 00 instead of 0, and the correct integer with no leading 0 otherwise.
if(empty($count[0])) echo 0; // Don't get why we need this--not needed on 000webhost, e.g.
echo $count[0];
Update: confirmed bug on ipage's end

Related

if not working with decrement/increment

Hi I´m new in php and in one book i found this:
$x = 0;
if ($x-- == 0) echo $x;
in book they write it should write -1 to my page, because decremenet was done after comparison and before the command exho was executed. I understand why it is that, but when I type it into my editor it's giving me error because it's understanding $x-- as one and $x as another variable. Where's problem?
Basic Steps :
Step 1: (get Notepad++)
Go to http://notepad-plus-plus.org/ and download Notepad++.
Notepad++ is a free (as in "free speech" and also as in "free beer") source code editor and Notepad replacement that supports several languages. Running in the MS Windows environment, its use is governed by GPL License.
Step 2: (type the code)
type and save as "filename.php" in "www directory"
<?php
$x = 0;
if ($x-- == 0) echo $x;
?>
Step 3: (enJoy !)
Check the result
Expected Output :
-1 // this is what i got! :D
$x-- is postdecrement that is the $x value will be still 0. when you went away from the statement then only the $x value should be change. so if statement is working fine. the echo value will be -1.
7-isnotbad answer is correct.

Echoing large string in PHP results in no output at all

I am helping to build a Joomla site (using Joomla 1.5.26). One of the pages are really really big. As a result, PHP just stops working without any error and all previously printed strings are ignored. There is no output at all. We have display_errors set to TRUE and error_reporting set to E_ALL.
I found the exact line where PHP breaks. It's in libraries/joomla/application/component/view.php:196
function display($tpl = null)
{
$result = $this->loadTemplate($tpl);
if (JError::isError($result)) {
return $result;
}
echo $result;
}
Some information:
Replacing echo $result; with echo strlen($result); works. The length of the string is 257759.
echo substr($result, 0, 103396); is printing partial content.
echo substr($result, 0, 103397); results in no output at all.
echo substr($result, 0, 103396) . "A"; results in no output at all. So splitting string into chunks is not a solution.
I have checked server performance during the execution of the script. CPU usage is 100% but there's plenty of memory left. PHP memory limit is 1024M. output_buffering is 4096 but I tried setting it to unreasonably high number - dies at exact same position. Server runs Apache 2.2.14-5ubuntu8.10 and PHP 5.3.2-1ubuntu4.18. PHP runs as fast_cgi module.
I have never experienced something like that and Google search results in nothing also. Have any of you experienced something like that and know the solution?
Thanks for reading!
Maybe try exploding the string and looping through each line.
You could also try this, found on php.net - echo:
<?php
function echobig($string, $bufferSize = 8192)
{
// suggest doing a test for Integer & positive bufferSize
for ($chars = strlen($string)-1, $start = 0;$start <= $chars; $start += $bufferSize) {
echo substr($string, $start, $bufferSize);
}
}
?>
Basically, it seems echo can't handle such large data in one call. Breaking it up somehow should get you where you need to go.
what about try using print_r rather than echo
function display($tpl = null)
{
$result = $this->loadTemplate($tpl);
if (JError::isError($result)) {
return $result;
}
print_r($result);
}
I have tested this on the CLI and it works fine with PHP 5.4.11 and 5.3.15:
$str = '';
for ($i=0;$i<257759;$i++) {
$str .= 'a';
}
echo $str;
It seems a reasonable assumption that PHP itself works fine, but that the output buffer is too large for Apache/fast_cgi. I would investigate the Apache config further. Do you have any special Apache settings?
May be that?
Try something like this
php_flag output_buffering On
Or try to turn on gzip in Joomla!
Or use nginx as reverse proxy or standalone server :^ )
It seems I solved the problem by myself. It was somewhat unexpected thing - faulty HTML formatting. We use a template for order page and inside there is a loop which shows all ordered products. When there were a few products, everything worked great but when I tried to do the same with 40 products, the page did break.
However I still don't understand why the server response would be empty with status code 200.
Thanks for answers, everybody!

exp() returns NaN stuff

Ok, I'm stuck. I have this PHP code:
echo exp(12), '<br/>';
echo exp(4.2);
just like on the PHP.net page. And what I have on the screen:
NAN
298.86740096706
but then there comes the weirdest thing ever. I thought that maybe there's some PHP.ini problem with numbers like 2.67e8 and so (I mean with the 'e' inside) or something. But then, when I changed the above code into:
echo (2.67e8), '<br/>';
echo exp(4.2);
suddenly I saw this whatever:
267000000
NAN
It's kind of WTF especially because of the last NAN, when first it was a quite normal, calm float 298.86740096706 but then just with no reason it went to hell replaced by NAN. Do you have any ideas? Please?
If true, this must be a bug in your PHP version. Please report it.
By the way, I'm unable to reproduce it on PHP 5.3.2. Running:
php -r 'do { $c = exp(4.2); echo "point "; } while (is_nan($c)); echo "$c\n";'
produces the expected output:
point 66.686331040925

sprintf bug with php & apache in windows?

I've run into a strange problem on a WAMP server setup (PHP version 5.3.0, Apache 2.2.11). When using sprintf to output a number, I occasionally get erroneous characters in the output string.
Example: (not trimmed from anything, this is the only code in the script)
$dt1 = new DateTime('now');
$dt2 = new DateTime('now - 10 min');
$interval = $dt1->diff($dt2);
$number = 10.0;
$string = sprintf("%.1f", $number);
echo "number: $number, string: $string\n";
If I run this at the command prompt with PHP CLI, I get the expected output:
number: 10, string: 10.0
However, if I serve it using Apache, in the browser I get
number: 10, string: :.0
with a colon where '10' should be. (Note that ':' is the next ascii character in sequence after '9', if $number is 0-9, everything works. Numbers greater than 10 appear to use ascii equivalents - so 11 is ';', 12 is '<', etc.)
The strangest part is that the first four lines in the above code sample seem to affect the results. Logically, those statements should have no impact, but if I comment them out or remove them the problem goes away.
Any ideas? Anyone else able to replicate this?
Notes:
I've tried php 5.3.1 and 5.3.2, both behave the same way
The above script works fine, even in the browser, for 5-6 page refreshes after restarting Apache. Then the error, as described, returns
Try adding this above the code setlocale(LC_ALL, 'en_US');
Try this:
Change echo "number: $number, string: $string\n"; to:
for ($i = 0, $n = strlen($string); $i < $n; $i++) {
echo ord($string[$i]).' ';
}
It will basically give you the numeric character code for each byte in the string. Note that I said byte. If it's a character set problem, or a problem with Apache mangling bytes, you should see that here. Expected output is: 49 48 46 48. If you instead see 58 46 48, then you indeed may have found a bug with php and should submit a bug report. You also should try upgrading your php version (5.3.2 is out)...

Seeking STDOUT in PHP

I have a php script that is running in CLI and I want to display the current percent progress so I was wondering if it is possible to update the STDOUT to display the new percent.
When I use rewind() or fseek() it just throws an error message.
See this code:
<?php
echo "1";
echo chr(8);
echo "2";
The output is only 2 since "chr(8)" is the char for "backspace".
So just print the amount of chars you need to go back and print the new percentage.
Printing "\r" works too on Linux and Windows but isn't going to cut it on a mac
Working example:
echo "Done: ";
$string = "";
for($i = 0; $i < 100; ++$i) {
echo str_repeat(chr(8), strlen($string));
$string = $i."%";
echo $string;
sleep(1);
}
Output \r and then flush to get back to the first column of the current line.
Writing to a console/terminal is surprisingly complex if you want to move backwards in the output raster or do things like add colours - and the behaviour will vary depending on the type of console/terminal you are using. A long time ago some people came up with the idea of building an abstract representation of a terminal and writing to that.
See this article for details of how to do that in PHP.

Categories