note 111636 deleted from language.operators.comparison by aharvey
| From: | aharvey@php.net | Date: | Mon, 11 Mar 2013 16:20:56 +0000 |
| Subject: | note 111636 deleted from language.operators.comparison by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-194032@lists.php.net to get a copy of this message | ||
Note Submitter: ludko2 at gmail dot com
----
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 :(