note 63568 deleted from language.types.array by bjori
| From: | bjori@php.net | Date: | Fri, 24 Mar 2006 08:56:09 +0000 |
| Subject: | note 63568 deleted from language.types.array by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-106517@lists.php.net to get a copy of this message | ||
Note Submitter: wvb3
Reason: in docs
----
One big hole in the documentation is what happens when you get the value of something which
doesn't exist in the array. Is it an errror? Does it return 0? Does it return NULL? Does it
create a new key in the array?
Here's an example:
-------
$a = array(
'foo' => 'bar',
'bif' => 'bop'
);
$a['zip'] = NULL;
echo "a['foo'] is ->{$a['foo']}<- \n";
echo "a['huh'] is ->{$a['huh']}<- \n";
echo "a['zip'] is ->{$a['zip']}<- \n";
print_r($a);
echo "Typeof a[huh] is ". gettype($a['huh']) ."\n";
echo "Typeof a[zip] is ". gettype($a['zip']) ."\n";
echo "isset(a[huh]) says ". (isset($a['huh'])?"SET":"NOT
SET") ."\n";
echo "isset(a[zip]) says ". (isset($a['zip'])?"SET":"NOT
SET") ."\n";
------------
The output of the above is:
a['foo'] is ->bar<-
a['huh'] is -><-
a['zip'] is -><-
Array
(
[foo] => bar
[bif] => bop
[zip] =>
)
Typeof a[huh] is NULL
Typeof a[zip] is NULL
isset(a[huh]) says NOT SET
isset(a[zip]) says NOT SET
--------------
So the answers are:
- It is NOT an error.
- It does NOT create a new key.
- The return value is NULL.
and
- isset can't tell the difference between an existing key set to NULL and one that truly is not
set.
Note that with the last point, iterating over the values WILL turn up the ones that exist but are
set to NULL, so watch out.
There are more comments worth looking at concerning arrays and NULLs over at this page:
http://jp2.php.net/manual/en/language.types.null.php