note 90844 deleted from function.unset by danbrown
| From: | danbrown@php.net | Date: | Tue, 12 May 2009 18:48:24 +0000 |
| Subject: | note 90844 deleted from function.unset by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154515@lists.php.net to get a copy of this message | ||
Note Submitter: LungZeno
----
'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();
?>