Possible Duplicate:
Strange behavior with isset() returning true for an Array Key that does NOT exist
$arr = array(
'application' => array (
'environment' => 'development',
'mode_debug' => 1,
'key' => '123456abcdefg',
'debug_soft' =>
'firephp',
'aaa' => array (
'bbb' => '111',
'ccc' => '222',
'ddd' => array (
'eee' => '12345',
),
),
),
'database' => array (
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
),
);
if(isset($arr["application"]["aaa"]["ddd"]["eee"]['out']) && !empty($arr["application"]["aaa"]["ddd"]["eee"]['out'])){
echo "a";
echo '<br />';
var_dump($arr["application"]["aaa"]["ddd"]["eee"]['out']);
}else{
echo "b";
}
returns:
a
string(1) "1"
but there is no ‘out’ index in the ‘$arr’ array, so why it returns ‘string(1) “1”‘?
The manual doesn’t help, because it only samples one dimentional array:
$a = array ('test' => 1, 'hello' => NULL);
var_dump( isset ($a['test']) ); // TRUE
var_dump( isset ($a['foo']) ); // FALSE
var_dump( isset ($a['hello']) ); // FALSE
Consider this:
It returns true, because:
PHP supports
$str[$n]syntax (to address individual characters of the string)when calculating index, it’s cast to integer type
when cast to integer,
"xxx"is0; you’re accessing$str[0]– the first character of that$strstring, and it (t) is a truthy value.Note that this (quite weird) behavior of isset was fixed in PHP 5.4: