PHP variable reference and variable destruction mechanism detailed introduction _php example

Source: Internet
Author: User

PHP variable reference and variable destruction mechanism

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

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

$a = "Hello world";//define a variable, the following assignment to $b
$b = $a;//This step does not precede $a with symbols 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

$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:

$a = "Hello World";
$b = & $a;
$b = "test new value";//change the value of B, the value of a will follow the
echo $a;//output test new value, because changing the value of B will also change the value of a.
=====================================
often see things like this when defining functions: Function
Test (& $param)
{
// The contents of the function definition
$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:

$k = 8;
Test ($k);
echo $k//The result $k value is changed by the function, output 9.
you will also often see such a call function:
$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.

<?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 does not delete
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

<?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, please refer to the previous exampleAnalyze the value of the $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 "A Long is $big _long_variable_name";?> <br/> <br/> <?php ($big unset); /* with unset() Destroy the variable $big_long_variable_name, the variable $short will not be affected by any effect.  */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 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"; /* Re-assign 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.

 */

Thank you for reading, I hope to help you, thank you for your support for this site!

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.