Analysis of variable reference and variable destruction mechanism in PHP _php techniques

Source: Internet
Author: User
Tags garbage collection

In this paper, we analyze the mechanism of variable reference and variable destruction in PHP. Share to everyone for your reference. The specific analysis is as follows:

Variable is a very important type in PHP, we have data through variables or constants to operate, the following to see the variable reference and variable destruction.

In PHP, the symbol "&" denotes a reference.

1, to see if the situation is not quoted this way:

Copy Code code as follows:
$a = "Hello world";//define a variable, the following assignment to $b
$b = $a///This step does not precede the $a with a symbol, such as "$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, the use of "=" direct assignment, in fact, is a copy of the right side of the variable to B, will generate a memory space, the result may be the same content in memory two. 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. Use the symbol & quote

Copy Code code as follows:
$a = "Hello World";
$b = & $a;

Using a reference, the PHP engine does not copy a variable, but instead points the pointer to the address in memory of the $a, which is saved $b.
So when you use a reference, you change the value of the $b, and $a change.
Like what:
Copy Code code as follows:
$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:

Copy Code code as follows:
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:
Copy Code code as follows:
$k = 8;
Test ($K);
echo $k//The result $k value is changed by the function, output 9.

You will also often see this kind of call function:
Copy Code code as follows:
$return = & Test_func ();

The mechanism of 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 concept of 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 are explained in the PHP manual as follows:

Referencing in PHP means accessing the contents of the same variable with a different name. 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, the time to destroy the variable. Does not change the original value.

Test: $b = & $a;
Since changing the value of the $b, the value of $a also follow the change, if the $b destroyed (in memory does not occupy space, not NULL, nor is the value of ""), $a value will also be deleted then?

In fact, there is a foreign book in PHP specifically mentioned 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 is to avoid the $b to delete, resulting in the $a also to delete the problem.

Copy Code code as follows:
<?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
Copy Code code as follows:
<?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, it was added after the end of the value. (dot), thus the value of variable $big_long_variable_name at this time should be the original value ("PHP") + The New value ("rocks!"), 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 it was added after the $short. (point number), so 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/>
<?php $short = "No point TEST1"; /* Re-assign the variable $short. Since this is not added after $short. (dot), so the current value of $short is "No point TEST1". */
Print "15/$short is $short."; /* Output: $short is No point TEST1. */
$short = "No point TEST2 $short"; /* Re-assign the variable $short. Not added after the $short. (point number), but refers to its own last value "No point TEST1". */
Print "16/$short is $short."; /* Output: $short is no. TEST2 no point TEST1. */

I hope this article will help you with your PHP program design.

Related Article

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.