|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-11-22 07:31 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'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 09:00:01 2025 UTC |
@radamanf: Stop shouting. Shouting does not help. I agree that this behavior ('foo' == 0) is counter-productive. I think most people would agree on that. But even if everyone agrees that it's the wrong behavior, changing it isn't so easy. Changing this behavior will probably break existing software. If you really want to change this, then there is only one way: Make the change and then test a shitload of code against it. See how many tests will fail in major PHP projects and how easy things are to fix. If you can provide sufficient data that this change (which goes rather deep into the core semantics of the language) won't affect existing projects heavily, then I see no problem with doing it.Here's the basic problem of logic that doesn't follow for me: If I do if(0) { //...is false, this will never run } and I do if("string") { //...is true, will always run } Then how the hell can true == falseHey xtalviper, the reason it does this is because in the case of: if($string) then the string is cast to a boolean. If you do: if($int == $string) then the string is cast to an int. You can see this more clearly by doing this: <?php $boolstring = (boolean)"string"; $intstring = (int)"string"; var_dump($boolstring); var_dump($intstring);*Sighh* Do I have to open a separate bug report ? Everybody seems to ignore the comment I made about a year ago about the fact that: ((string) "10" == (string) "1e1") => true So okay, ("test" == 0) => true, let's pretend it's a feature (and insult the intelligence of about every PHP developer) but there is no way that a comparison between two strings could end up being evaluated as numbers ! There is clearly something very wrong with that !I'm sorry, but this is just silly. This has to be one of PHP's most glaring weaknesses. While it can be understood, this does NOT make it logical. In fact, unless you have a very firm understanding of both how PHP casts strings to integers, and always explicitly know or cast your types (which PHP's design largely tries to avoid having users worry about), this becomes an issue that can hit somebody out of nowhere, unexpectedly, and leave them wracking their brains trying to sort out. root@host:/bright/Bright# php -r "echo (0 == 'string') ? 'TRUE' : 'FALSE';" TRUE root@host:/bright/Bright# php -r "echo (0 == '1string') ? 'TRUE' : 'FALSE';" FALSE root@host:/bright/Bright# php -r "echo ('0' == 'string1') ? 'TRUE' : 'FALSE';" FALSE root@host:/bright/Bright# php -r "echo (1 == 'string') ? 'TRUE' : 'FALSE';" FALSE root@host:/bright/Bright# php -r "echo (1 == '1string') ? 'TRUE' : 'FALSE';" TRUE root@host:/bright/Bright# php -r "echo (1 == 'string1') ? 'TRUE' : 'FALSE';" FALSE I realize and understand that this may never change, but that doesn't make it logical or correct, and in the meantime you're probably allowing more bugs in the wild than you would cause by actually fixing the issue or using one of the other suggestions from this thread (such as throwing a Notice).I have to agree with the above, this is illogical and confusing. What just caught me out was an internal conversion from string to int which triggered the bug (and yes I definitely consider this a bug.) Try this: <?php $arr = array('0' => 'Abc'); $keys = array_keys($arr); if ($keys[0] == 'foo') { echo 'String\'s equal'; } ?>