PHP Tutorial Variable Destruction unset usage
Unset--releasing the given variable
Describe
void unset (mixed var [, mixed Var [, ...]])
Unset () destroys the specified variable. Note in PHP 3, unset () returns True (actually an integer value of 1), whereas in PHP 4, unset () is no longer a real function: it is now a statement. Thus there is no return value, and attempting to get the return value of unset () will result in a parse error.
Refer to the PHP manual:
/* Imagine This is memory map
______________________________
|pointer | Value | Variable |
-----------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- |
| 3 | NULL | --- |
| 4 | NULL | --- |
| 5 | NULL | --- |
------------------------------------
Create some variables */
$a = 10;
$b = 20;
$c =array (' One ' =>array (1, 2, 3));
/* Look at memory
_______________________________
|pointer | Value | Variable ' s |
-----------------------------------
| 1 | 10 | $a |
| 2 | 20 | $b |
| 3 | 1 | $c [' One '][0] |
| 4 | 2 | $c [' One '][1] |
| 5 | 3 | $c [' One '][2] |
------------------------------------
Do */
$a =& $c [' one '][2];
/* Look at memory
_______________________________
|pointer | Value | Variable ' s |
-----------------------------------
| 1 | NULL | --- | Value of $a is destroyed and pointer are free
| 2 | 20 | $b |
| 3 | 1 | $c [' One '][0] |
| 4 | 2 | $c [' One '][1] |
| 5 | 3 | $c [' One '][2], $a | $a is now here
------------------------------------
Do */
$b =& $a; or $b =& $c [' one '][2]; Result is same as both "$c [' One '][2]" and "$a" are at same pointer.
/* Look at memory
_________________________________
|pointer | Value | Variable ' s |
--------------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- | Value of $b is destroyed and pointer are free
| 3 | 1 | $c [' One '][0] |
| 4 | 2 | $c [' One '][1] |
| 5 | 3 | $c [' one '][2], $a, $b | $b is now here
---------------------------------------
Next do */
unset ($c [' One '][2]);
/* Look at memory
_________________________________
|pointer | Value | Variable ' s |
--------------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- |
| 3 | 1 | $c [' One '][0] |
| 4 | 2 | $c [' One '][1] |
| 5 | 3 | $a, $b | $c [' One '][2] is destroyed not in memory, not in array
---------------------------------------
Next do */
$c [' One '][2]=500; Now it's in array
/* Look at memory
_________________________________
|pointer | Value | Variable ' s |
--------------------------------------
| 1 | 500 | $c [' One '][2] | Created it lands on No (next) free pointer in memory
| 2 | NULL | --- |
| 3 | 1 | $c [' One '][0] |
| 4 | 2 | $c [' One '][1] |
| 5 | 3 | $a, $b | This pointer are in use
---------------------------------------
Lets tray to return $c the [' One '][2] at the old pointer a remove reference $a, $b. * *
$c [' One '][2]=& $a;
unset ($a);
Unset ($b);
/* Look at memory
_________________________________
|pointer | Value | Variable ' s |
--------------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- |
| 3 | 1 | $c [' One '][0] |
| 4 | 2 | $c [' One '][1] |
| 5 | 3 | $c [' One '][2] | $c [' One '][2] is returned, $a, $b is destroyed
---------------------------------------? >
I hope this helps tutorial.
This will explain how PHP's unset is carried out.
The first thing to emphasize is that unset in PHP is no longer a function, since it is not a function, then there is no return value, so use the time can not be enough to unset the return value to make judgments.
Second, in the function, unset can only destroy local variables, and can not destroy the global variables, see an example of the manual
function Destroy_foo () {
Global $foo;
Unset ($foo);
}
$foo = ' Bar ';
Destroy_foo ();
Echo $foo;
?>
The returned result is
Bar
Http://www.bkjia.com/phper/php/37201.htm
http://www.bkjia.com/PHPjc/445335.html www.bkjia.com true http://www.bkjia.com/PHPjc/445335.html techarticle PHP Tutorial Variable destruction unset usage unset--Releases the given variable description void unset (mixed var [, mixed Var [, ...]]) unset () destroys the specified variable. Note in PHP 3, unset ( ...