Created
April 12, 2012 20:27
-
-
Save ejdanderson/2370749 to your computer and use it in GitHub Desktop.
String Evaluates to 0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$arr = array(); | |
$arr['foo'] = 'bar'; | |
// 'boo' evaluates to 0 | |
echo $arr['foo']['boo']; // b | |
// This evaluates as true and echos | |
if (isset($arr['foo']['boo'])) { | |
echo 'FooBoo'; | |
} | |
// What was intended. Does not echo | |
if (isset($arr['foo']) && is_array($arr['foo']) && isset($arr['foo']['boo'])) { | |
echo 'This is what I really meant'; | |
} | |
// Evaluates as false, '10' treated as its numeric value, does not echo | |
if (isset($arr['foo']['10'])) { | |
echo 'FooBoo2'; | |
} | |
// Evaluates as true, '1' treated as its numeric value, echos | |
if (isset($arr['foo']['1'])) { | |
echo $arr['foo']['1']; //a (second character in 'bar') | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment