Php variable destruction unset usage. Php Tutorial variable destruction unset usage unset -- release the given variable description voidunset (mixedvar [, mixedvar [,...]) unset () destroy the specified variable. Note: in PHP3, unset (php Tutorial variable destruction unset usage
Unset -- release the specified variable
Description
Void unset (mixed var [, mixed var [,...])
Unset () destroys the specified variable. Note that in PHP 3, unset () will return TRUE (actually an integer value of 1). in PHP 4, unset () is no longer a real function: it is now a statement. In this way, no return value is returned. an attempt to obtain the return value of unset () will cause a parsing 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 is 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" is at same pointer.
/* Look at memory
_________________________________
| Pointer | value | variable's |
--------------------------------------
| 1 | NULL | --- |
| 2 | NULL | --- | // value of $ B is destroyed and pointer is 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 is in array
/* Look at memory
_________________________________
| Pointer | value | variable's |
--------------------------------------
| 1 | 500 | $ c ['one'] [2] | // created it lands on any (next) free pointer in memory
| 2 | NULL | --- |
| 3 | 1 | $ c ['one'] [0] |
| 4 | 2 | $ c ['one'] [1] |
| 5 | 3 | $ a, $ B | // this pointer is in use
---------------------------------------
Lets tray to return $ c ['one'] [2] at old pointer an remove reference $ a, $ B .*/
$ C ['one'] [2] = & $;
Unset ($ );
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 shows how php unset is implemented.
It should be emphasized that unset is no longer a function in php. since it is not a function, no return value is returned. Therefore, the unset return value cannot be used for determination.
Second, in a function, unset can only destroy local variables, and cannot destroy global variables. let's look at an example in the following 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
Revoke unset -- release the specified variable description void unset (mixed var [, mixed var [,...]) unset () to destroy the specified variable. Note that in PHP 3, unset (...