note 63568 added to language.types.array

From: Date: Fri, 24 Mar 2006 00:45:30 +0000
Subject: note 63568 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-106506@lists.php.net to get a copy of this message
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 ---- Server IP: 210.138.109.182 Probable Submitter: 202.226.189.178 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/note/edit/63568 Del: integrated -- http://master.php.net/note/delete/63568/integrated Del: useless -- http://master.php.net/note/delete/63568/useless Del: bad code -- http://master.php.net/note/delete/63568/bad+code Del: spam -- http://master.php.net/note/delete/63568/spam Del: non-english -- http://master.php.net/note/delete/63568/non-english Del: in docs -- http://master.php.net/note/delete/63568/in+docs Del: other reasons-- http://master.php.net/note/delete/63568 Reject -- http://master.php.net/note/reject/63568 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106506) next »