ID: 40115
User updated by: jannisbreitwieser at web dot de
Reported By: jannisbreitwieser at web dot de
-Status: Bogus
+Status: Open
Bug Type: Strings related
Operating System: Linux 2.6
PHP Version: 5.2.0
New Comment:
I´m afraid the answert doesn´t explain php´s odd behavior.
Just run the code on the bottom and try to figure out its behavior.
It´s definitely not as it should be!
String-String Comparisons are handley incorrectly internally somehow.
Int-Int Comparisons also work without being affected by overflows.
Run This:
/////////////////////////////
<?PHP
error_reporting ( 63 );
echo "\n";
$a = 2147483648;
var_dump($a);
$a = 2147483647;
var_dump($a);
$a = -2147483648;
var_dump($a); // WTF?
$a = -2147483647;
var_dump($a);
echo "\n";
function print_results( $a, $b ) {
$c = $a - $b;
// var_dump($a);
// var_dump($b);
echo sprintf( " %7s>%7s", gettype($a), gettype($b) ) .
" | " . ( $a>$b ? "true " : "false" ) .
" | " . "a-b = "; var_dump($c);
}
echo '$a as an int - unexpected results ';
for ($b = -2; $b <= 2; $b+=1) {
$a = '-2147483647'; // the strings number value of $a is an int for
php - see above
$b = (string)$b;
echo "$a>$b:\n";
echo "\n";
$a = (string)$a;
$b = (string)$b;
print_results( $a, $b );
$a = (int)$a;
$b = (string)$b;
print_results( $a, $b );
$a = (string)$a;
$b = (int)$b;
print_results( $a, $b );
$a = (int)$a;
$b = (int)$b;
print_results( $a, $b );
echo "\n";
}
echo '$a as a fake float - unexpected results';
for ($b = -2; $b <= 2; $b+=1) {
$a = '-2147483648'; // This time $a is a float for php - still wrong
result, probably its internally handled as an int since pow(2,-31) is
still in the int range of normal programming languages like c
$b = (string)$b;
echo "$a>$b:\n";
echo "\n";
$a = (string)$a;
$b = (string)$b;
print_results( $a, $b );
$a = (int)$a;
$b = (string)$b;
print_results( $a, $b );
$a = (string)$a;
$b = (int)$b;
print_results( $a, $b );
$a = (int)$a;
$b = (int)$b;
print_results( $a, $b );
echo "\n";
}
echo '$a as a real float - results as expected';
for ($b = -2; $b <= 2; $b+=1) {
$a = '-2147483649'; // This time the number value of $a is a "real"
float since it is out of a normal integers range. - results are as
expected.
$b = (string)$b;
echo "$a>$b:\n";
echo "\n";
$a = (string)$a;
$b = (string)$b;
print_results( $a, $b );
$a = (int)$a;
$b = (string)$b;
print_results( $a, $b );
$a = (string)$a;
$b = (int)$b;
print_results( $a, $b );
$a = (int)$a;
$b = (int)$b;
print_results( $a, $b );
echo "\n";
}
?>
Previous Comments:
------------------------------------------------------------------------
[2007-01-12 22:23:11] [EMAIL PROTECTED]
Integers tend to overflow and this is expected.
------------------------------------------------------------------------
[2007-01-12 22:19:44] jannisbreitwieser at web dot de
Description:
------------
<?
$a = '-2147483648';
for ($a = -2147373648 ; $a > -2147483670; $a-=1000) {
$a = (string) $a;
$b = '50000';
echo $a > $b ? "$a - true\n" : "$a - false\n";
}
?>
-> The comparison should always return false but it doesnt - for some
reason some of the comparisons return true.
Reproduce code:
---------------
according to
http://www.php.net/manual/en/language.operators.comparison.php
Quote:"If you compare two numerical strings, they are compared as
integers."
This is not the case since
$a = '-2147483648';
for ($a = -2147373648 ; $a > -2147483670; $a-=1000) {
// No longer cast to string
$b = '50000';
echo $a > $b ? "$a - true\n" : "$a - false\n";
}
returns different values (which is 'false' as expected).
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=40115&edit=1