sprintf bug with php & apache in windows?
Posted
by potatoe
on Stack Overflow
See other posts from Stack Overflow
or by potatoe
Published on 2010-06-08T20:24:47Z
Indexed on
2010/06/09
17:02 UTC
Read the original article
Hit count: 145
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?
© Stack Overflow or respective owner