#39579 [Bgs->Opn]: Comparing zero & string values in boolean comparison has unexpected behaviour
| From: | iain at workingsoftware dot com dot au | Date: | Wed, 22 Nov 2006 08:35:23 +0000 |
| Subject: | #39579 [Bgs->Opn]: Comparing zero & string values in boolean comparison has unexpected behaviour | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-105286@lists.php.net to get a copy of this message | ||
ID: 39579
User updated by: iain at workingsoftware dot com dot au
Reported By: iain at workingsoftware dot com dot au
-Status: Bogus
+Status: Open
-Bug Type: Class/Object related
+Bug Type: Variables related
Operating System: FreeBSD 6.1
PHP Version: 5.2.0
New Comment:
well it seems to me that at least a warning should be given when
comparing an integer 0 to a non-integer value that evaluates to 0 when
cast as an int unless an explicit type cast is used without using
strict equals.
i can see why this behaviour is not a bug, because (int)'SOME STRING'
== 0, but it's also has the potential to cause unexpected results/bugs
unless you use strict equals everywhere.
in this case, i fixed the problem by using === instead of ==, but it
took a bit of time to track down the error because there was no warning
or anything. i think that it would be better to require someone to do:
if($value == (int)'SOME STRING')
OR
if($value === 'SOME STRING')
in order to avoid a warning being emmited if $value == 0.
Previous Comments:
------------------------------------------------------------------------
[2006-11-22 08:17:01] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
.
------------------------------------------------------------------------
[2006-11-22 07:31:34] iain at workingsoftware dot com dot au
Description:
------------
if you have a variable with the value 0 assigned to it and do a
comparison with a non-integer then the non-integer value is cast to an
int with unexpected results. it behaves differently when comparing a
non-zero integer.
Reproduce code:
---------------
put this in test.php and run php -f test.php:
<?php
$zero = 0;
$one = 1;
if($zero == 'SOME STRING')
echo("0 does equal 'SOME STRING'\n");
else
echo("0 does not equal 'SOME STRING'\n");
if($one == 'SOME STRING')
echo("1 does equal 'SOME STRING'\n");
else
echo("1 does not equal 'SOME STRING'\n");
?>
Expected result:
----------------
0 does not equal 'SOME STRING'
1 does not equal 'SOME STRING'
Actual result:
--------------
0 does equal 'SOME STRING'
1 does not equal 'SOME STRING'
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=39579&edit=1