note 47396 deleted from function.isset by cmb
| From: | cmb@php.net | Date: | Sun, 05 Jan 2020 08:08:11 +0000 |
| Subject: | note 47396 deleted from function.isset by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213733@lists.php.net to get a copy of this message | ||
Note Submitter: jc dot michel at symetrie dot com
----
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