note 90844 added to function.unset

From: Date: Tue, 12 May 2009 18:46:47 +0000
Subject: note 90844 added to function.unset
Groups: php.notes 
Request: Send a blank email to php-notes+get-154514@lists.php.net to get a copy of this message
'unset' does not always destory variable value. It always destorys variable key, but only destorys variable value which no other things refer to. Every scope variable is a key in scope array, but that key refers to a value. <?php function fun(){ $a = 'aa'; debug_zval_dump(&$a); // fetch value which local scope variable key 'a' refers to, and let first argument of this call refers to that value 'aa', so refcount increases one. $a = $a2; // fetch value which local scope variable key 'a2' refers to, and let local scope variable key 'a2' refers to the copy of that value. Local scope variable key 'a2' does not exist before, so create a new value NULL. This value is only for copying, so no key will be created. $a = &$a3; // fetch value which local scope variable key 'a3' refers to, and let local scope variable key 'a3' refers to that value. Local scope variable key 'a3' does not exist before and this fetching is for referring, so create local scope variable key 'a3' and a new value NULL and let this key 'a3' refers to the new value. var_dump(get_defined_vars()); debug_zval_dump(&$a4); // like above case, create local scope variable key 'a4' and a new value NULL and let this key 'a4' refers to the new value. var_dump(get_defined_vars()); } echo "\n"; fun(); $b = 'bb'; $b2 = 'bb2'; function fun2(){ global $b; // fetch value which global scope variable key 'b' refers to, and let local scope variable key 'b' refers to that value 'bb'. Local scope variable key 'b' does not exist before, so create it. debug_zval_dump(&$b); $b2 = 'bb3'; $b3 = &$b2; debug_zval_dump(&$b2); debug_zval_dump(&$b3); global $b2; // fetch value which global scope variable key 'b2' refers to, and let local scope variable key 'b2' refers to this value 'bb2'. Value which local scope variable key 'b3' refers to does not change and is still 'bb3', but its refcount decreases one. debug_zval_dump(&$b2); debug_zval_dump(&$b3); unset($b2); // destorys local scope variable key 'b2'. refcount of value 'bb2' which global scope variable key 'b2' refers to decreases one. var_dump(get_defined_vars()); debug_zval_dump(&$GLOBALS['b2']); global $b4; // fetch value which global scope variable key 'b4' refers to, and let local scope variable key 'b4' refers to that value. Global scope variable key 'b4' does not exist before and this fetching is for referring, so create global scope variable key 'b4' and a new value NULL and let this global variable key 'b4' refers to the new value. Local scope variable key 'b4' does not exist before, so create it. var_dump(get_defined_vars()); var_dump($GLOBALS); } echo "\n"; fun2(); function fun3(){ static $c = 'cc'; // fetch value which key 'c' in static variable array of function fun3 refers to which was initialized to value 'cc' in compile-time, and let local scope variable key 'c' refers to that value. Local scope variable key 'c' does not exist before, so create it. debug_zval_dump(&$c); } echo "\n"; fun3(); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 123.202.95.244 ---- Manual Page -- http://www.php.net/manual/en/function.unset.php Edit -- https://master.php.net/note/edit/90844 Del: integrated -- https://master.php.net/note/delete/90844/integrated Del: useless -- https://master.php.net/note/delete/90844/useless Del: bad code -- https://master.php.net/note/delete/90844/bad+code Del: spam -- https://master.php.net/note/delete/90844/spam Del: non-english -- https://master.php.net/note/delete/90844/non-english Del: in docs -- https://master.php.net/note/delete/90844/in+docs Del: other reasons-- https://master.php.net/note/delete/90844 Reject -- https://master.php.net/note/reject/90844 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154514) next »