note 47396 added to function.isset
| From: | jc dot michel at symetrie dot com | Date: | Mon, 15 Nov 2004 10:35:03 +0000 |
| Subject: | note 47396 added to function.isset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-80536@lists.php.net to get a copy of this message | ||
Using
isset($array['key'])
is useful, but be careful!
using
isset($array['key']['subkey'])
doesn't work as one could expect, if $array['key'] is a string it seems that
'subkey' is converted to (integer) 0 and $array['key']['subkey'] is
evaluated as the first char of the string.
The solution is to use
is_array($array['key']) && isset($array['key']['subkey'])
Here is a small code to show this:
<?php
$ex = array('one' => 'val1','two' => 'val2');
echo '$ex = ';print_r($ex);
echo "<br />";
echo " isset(\$ex['one']['three']) : ";
if (isset($ex['one']['three']))
echo 'true';
else
echo 'false';
echo "<br />";
echo "is_array(\$ex['one']) &&
isset(\$ex['one']['three']) : ";
if (is_array($ex['one']) && isset($ex['one']['three']))
echo 'true';
else
echo 'false';
?>
shows:
$ex = Array ( [one] => val1 [two] => val2 )
isset($ex['one']['three']) : true
is_array($ex['one']) && isset($ex['one']['three']) : false
----
Manual Page -- http://www.php.net/manual/en/function.isset.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47396
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47396&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47396&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47396&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47396&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47396&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47396&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47396&report=yes
Search -- http://master.php.net/manage/user-notes.php