A detailed explanation of variable reference and variable destruction mechanism in PHP

Source: Internet
Author: User
Tags garbage collection

In PHP, the symbol "&" represents a reference.
1, see what does not refer to this:
$a   = "Hello world";//define a variable, the following assignment to $b
$b = $a;//This step does not precede $a with a symbol Like this "$b = & $a". No Plus, in fact, the principle is to copy the variable $a copies, that is, in memory to reapply an address storage variable $b
PS: in php, use "=" direct assignment, in fact, is a copy of the right variable to B, will generate a memory space, The result may be two copies of the same content in memory. In some aspects of PHP performance, this will occupy more memory space. However, in my contacts, most people do not pay much attention to, in fact, the general application of this kind of use of the obvious difference is not obvious. will not see what effect, in fact, I do not often use & to quote, hehe. It's just that I think it's very necessary to get a thorough understanding of how it works. I like to focus on what is original and rational.
2, using symbol & to refer to
$a   = "Hello World";
$b = &  $a;
using references, PHP engine does not copy a variable. In fact, the pointer is pointed to the $a in memory address, $b is to save this pointer.
So when you use a reference, you change the value of the $b, and the $a changes accordingly
such as:

The code is as follows Copy Code
$a = "Hello World";
$b = & $a;
$b = "test new value";//change the value of B, the value of a will follow.
echo $a//output test new value, because changing the value of B also changes the value of a.
=====================================
You often see things like this when you define a function:
Function test (& $param)
{
What the function defines
$param + +;
}

Explanation: $param preceded by a reference, the incoming parameter does not copy a copy in memory, but instead directly references the original memory space. So: If you modify the value of the variable used in the symbol & incoming, you will also change the value in the original memory space.
Take a quiz as follows:

  code is as follows copy code
$k = 8;
Test ($K);
Echo $k//The value of the result $k is changed by the function, outputting 9. The
also frequently sees such child invocation functions:
$return   = & Test_func ();

The

Previous mechanism for the PHP engine is: = will copy the contents of the right to the left side of the variable. So using & is not going to copy the result of the function, in fact my understanding is to give the pointer to the variable on the left.
What is a pointer, before learning the concepts in C language. My understanding is: pointers, pointers, pointing needles (compass, hehe). The pointer as a memory of the address easy to understand the point, the computer will know where to find the memory location data. This is plain understanding, deep I will not, hehe.
Summary: Use references to reduce the footprint of memory resources.
The references in the PHP manual are interpreted as follows:
referencing in PHP means accessing the same variable content with different names. This is not like a C pointer, but instead, the reference is a symbolic table alias. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be viewed as hardlink in Unix file systems.
3, when the variable is destroyed. Does not change the original value.
Test: $b = & $a;
Now that you change the value of the $b, the value of $a changes, and if you destroy the $b (memory does not occupy space, not NULL, nor value is ""), $a value will also be deleted then?
In fact, there is a foreign book in PHP specifically referred to this mechanism. It was seen 2 years ago. I don't remember it very much. The principle is that when you delete a variable, it is automatically copied.
In fact this way, is to avoid the $b to delete, resulting in the $a also to delete the problem.

The code is as follows Copy Code
<?php
$a = ' d ';
$b = & $a;
$b = 8;//because it is a reference, so get rid of the value of B, the value of a is then changed to 8.
Var_dump ($b, $a);
Unset ($b);//Call unset Delete b variable, a variable will not be deleted
Var_dump ($b, $a);//output NULL and 8

Call unset Delete $b variable, the PHP engine from the variable symbol table found: I want to delete the variable $b original is a reference to the variable $a, this is not easy to delete ah, because a deletion of the $a variable is not, so the first copy of the $a variable after the deletion of the $b variable.
About PHP symbol table: In fact, my understanding is that all the variable names in the run are recorded in the inside, PHP to maintain, the specific data is stored in memory, of course, PHP is based on this symbol table to reclaim the useless space, free memory space. Take a look at the PHP garbage collection mechanism (releasing unused memory space), which is based on the symbol table.

Example

The code is as follows Copy Code

<?php
$long = "Big_long_variable_name";
$ $long = "PHP"; /* Use the string stored in the variable $long as the variable name for the new variable, equivalent to $big_long_variable_name= "PHP"; */
$short =& $big _long_variable_name; /* Take variable $big_long_variable_name value assigned to variable $short, at this time $short value is "PHP", equivalent to $short=& $ $long; */
Print "01/$short is $short."; /* "/$" is an escape sequence that represents the output of a dollar sign $, hereinafter. The function of this statement is output: $short is PHP. */
Print "Long is $big _long_variable_name."; /* Output: A Long is PHP. */
?>

<br/>
<br/>

<?php $big _long_variable_name.= "rocks!"; /* Re-assign values to $big_long_variable_name. During the $big_long_variable_name, the value of the variable $big_long_variable_name at this point should be the original value ("PHP") + The New value ("rocks!") as a result of the addition of the. (point number). , that is, the variable $big_long_variable_name the current complete value is "PHP rocks!". Same */
Print "03/$short is $short"; /* Output: $short is PHP rocks! */
Print "Long is $big _long_variable_name"; /* Output: Long is PHP rocks! */
?>

<br/>
<br/>

$short is PHP rocks!
The Long is PHP rocks!

<br/>
<br/>

<?php $short. = "Programming $short"; /* Re-assign the variable $short. Because you added a. (point number) after $short, refer to the above example to analyze the value of $short. */
Print "07/$short is $short"; /* Output: Modified $short is PHP rocks! Programming PHP rocks! */
Print "Long is $big _long_variable_name"; /* Because the variable $short is assigned to programming PHP rocks!, the value of the variable $big_long_variable_name is also changed to "PHP $short" with rocks! Programming PHP rocks! ". Output of this statement: Long is PHP rocks! Programming PHP rocks! Note that if a variable with the same value is destroyed unset (), the other variable does not apply to this case and is not destroyed together. */
?>

<br/>
<br/>

$short is programming PHP rocks!
A Long is programming PHP rocks!

<br/>
<br/>

<?php $big _long_variable_name.= "Web programming $short"; /* Variable $big_long_variable_name is assigned a value, at which point its full value should be PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks!. Variable $short on this time and variable $big_long_variable_name consistent. Please refer to the 5th and 10th notes for analysis respectively. */
Print "11/$short is $short"; /* Output: one PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks! */
Print "Long is $big _long_variable_name";
?>

<br/>
<br/>

<?php
unset ($big _long_variable_name); /* Use unset () to destroy variable $big_long_variable_name, the variable $short will not be affected by any. */
Print "13/$short is $short"; /* Although the variable $big_long_variable_name was destroyed, the $short is not affected, and its value is still the last given PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks! */
Print "Long is $big _long_variable_name."; /* Variable $big_long_variable_name has been destroyed and therefore has no value. Output:. */
Snow
?>

<br/>
<br/>

The

<?php $short = "No point TEST1";    /* assigns a value to a variable $short. Since it was not added after $short this time, the $short current value is "no point TEST1". */
Print "15/$short is $short.";    /* output: $short is No point TEST1. */
$short = "No point TEST2 $short";    /* the variable $short. did not add a. (dot) after $short, but referred to its own last value "no point TEST1". */
Print "16/$short is $short.";    /* output: $short is no. TEST2 no point TEST1. */

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.