PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #39579 Comparing zero & string values in boolean comparison has unexpected behaviour
Submitted:22 Nov 2006 7:31am UTC Modified: 22 Nov 2006 11:36am UTC
From:iain at workingsoftware dot com dot au Assigned to:
Status:Bogus Category:Variables related
Version:5.2.0 OS:FreeBSD 6.1
View/Vote Developer Edit Submission

[22 Nov 2006 7:31am UTC] 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'
[22 Nov 2006 8:17am UTC] 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

.
[22 Nov 2006 8:35am UTC] iain at workingsoftware dot com dot au
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.
[22 Nov 2006 9:39am UTC] johannes@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

That's how PHP works and it won't change.
[22 Nov 2006 11:06am UTC] mgf@php.net
And, besides, this behaviour is documented at
http://www.php.net/manual/en/language.types.string.php#language.types.st
ring.conversion
[22 Nov 2006 11:14am UTC] iain at workingsoftware dot com dot au
it's not the behaviour of how a string is cast to an integer that i'm
talking about, but if i have a comparison:

if($value == Class::CONSTANT)

and the class constant is a string, it's not immediately apparent that
if $value == 0 then this will evaluate to true. maybe warning is too
strong, but a notice might be good in the event that a non-strict ==
operation returns true because one of the operands is 0 and the other
operand is a value that evaluates to 0 when cast as an int.

anyway, i guess if php has been around for this long without anyone
mentioning it yet ... i mean, this is the first time i've come across
the problem. if a notice had been emitted it would have been a time
saver.
[22 Nov 2006 11:36am UTC] mgf@php.net
It's not a problem -- it's a feature, and it's documented at the address
I've just quoted, which describes evaluation of a string in any numeric
context.

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC