开发者

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)...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜