note 111636 added to language.operators.comparison
| From: | ludko2 at gmail dot com | Date: | Mon, 11 Mar 2013 10:35:45 +0000 |
| Subject: | note 111636 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194024@lists.php.net to get a copy of this message | ||
I was quite surprised with the revelation that zero number is equal to any string. I tried to submit
bug report, but it should be probably standard PHP behavior: https://bugs.php.net/bug.php?id=64376
So everyone who reads this: Please be aware, that if you relay on PHP arrays, and you want to use
string "0" as the key and you want to compare it to some value, it will always return
true.
So if you make the html/php like this:
<select name="n">
<?php
$data=array("0"=>"null","1"=>"key");
foreach($data as $k=>$v){
echo '<option ';
if($_REQUEST["n"] == $k) echo ' selected="selected"';
echo 'value="'.$k.'">'.$v.'</option>';
}
</select>
and then when you try to execute it, you will get selected the first item always!
As i said, PHP developers believe this should be normal behavior, so i believe this should be
commented properly.
Btw. Can anybody explain why var_dump(0=="a"); returns true? I am probably too dumb for
not seeing a logic in it :(
----
Server IP: 69.147.83.199
Probable Submitter: 85.237.224.58
----
Manual Page -- http://php.net/manual/en/language.operators.comparison.php
Edit -- https://master.php.net/note/edit/111636
Del: integrated -- https://master.php.net/note/delete/111636/integrated
Del: useless -- https://master.php.net/note/delete/111636/useless
Del: bad code -- https://master.php.net/note/delete/111636/bad+code
Del: spam -- https://master.php.net/note/delete/111636/spam
Del: non-english -- https://master.php.net/note/delete/111636/non-english
Del: in docs -- https://master.php.net/note/delete/111636/in+docs
Del: other reasons-- https://master.php.net/note/delete/111636
Reject -- https://master.php.net/note/reject/111636
Search -- https://master.php.net/manage/user-notes.php