Php variable destruction unset usage _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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 (...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.