Run this, I will wait
<?php $items = array('name'=>'blah1', 0=>array('blah2')); $name = 'name'; foreach($items as $key=>$value){ var_dump($key); var_dump($name); var_dump($name==$key); print "\n"; } ?> |
…
…
…
…
…
…
…
Probably not what you would expect from that is it?
Well, the first one should be:
string(4) "name" string(4) "name" bool(true) |
But the second one seems a little odd… at least for a moment anyway:
int(0) string(4) "name" bool(true) |
But then, you should probably rtfm:
If you compare an integer with a string, the string is converted to a number. If you compare two numerical strings, they are compared as integers. These rules also apply to the switch statement. The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an ‘e’ or ‘E’ followed by one or more digits.
Doh! Not really a wtf… more like a gotcha. Change the last comparison to check if they are Identical and you will be fine.
<?php $items = array('name'=>'blah1', 0=>array('blah2')); $name = 'name'; foreach($items as $key=>$value){ var_dump($key); var_dump($name); var_dump($name===$key); print "\n"; } ?> |
Stupid weakly typed languages… well… at least… stupid PHP, Ruby works fine:
x = rand(36**9).to_s(36) y = 2 puts x puts y puts(x == y) #false |