Skip to content

Instantly share code, notes, and snippets.

@ejdanderson
Created April 12, 2012 20:27
Show Gist options
  • Save ejdanderson/2370749 to your computer and use it in GitHub Desktop.
Save ejdanderson/2370749 to your computer and use it in GitHub Desktop.
String Evaluates to 0
<?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