PHP variable reference and variable destruction mechanism detailed

Source: Internet
Author: User
Variables are a very important type of PHP, we have data through variables or constants to operate, the following to see the variable reference and variable destruction bar, the need for friends can refer to the next

PHP variable reference and variable destruction mechanism

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

1. Look at the non-quoted situation is this:

$a = "Hello world";//define a variable, assign the following value to $b$b = $a;//This step is not preceded by a $ A sign &, like this "$b = & $a". Without &, the principle is that the variable $ A will be copied one copy, that is, in memory re-request an address storage variable $b

PS: in PHP, the use of "=" direct assignment, is actually copying a copy of the right variable to B, will generate a memory space, the result may be the same content in memory two copies. In some of the performance aspects of PHP mentioned, this way will occupy more memory space. But in my contact, most people do not pay much attention to, in fact, the general application of this kind of a significant difference in the production is not obvious. Do 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 deeper understanding of the implementation principles inside. I like to focus on the things that are original and rational.

2. Use symbols & References

$a = "Hello world"; $b = & $a;

Using a reference, the PHP engine does not copy a variable, but simply points the pointer to $ A in the in-memory address, 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 also changes echo $a;//output test new value, because changing the value of B also changes the value of a. ===================================== often see situations like this when defining functions: Function Test (& $param) {//function definition content $param++;}

Explanation: The $param is preceded by a reference, so incoming parameters are not copied in memory, but are referenced directly to the original memory space. So: If you modify the value of the variable used in symbols & incoming variables, the value in the original memory space will also be changed.

Take a quiz as follows:

$k = 8;test ($k); echo $k;//result the value of $k is changed inside the function, output 9. It is also common to see such calling functions: $return = & Test_func ();

The previous mechanism to understand the PHP engine is: = will copy the contents of the right to the left side of the variable. So using & is not a copy of the result of the function, in fact my understanding is to give the pointer to the variable on the left.

What is pointers, previously learned in C language concepts. My understanding is: pointers, pointers, pointing needles (compass, hehe). Think of the pointer as a memory address easy to understand point, the computer will know where to go to the memory where to find data. This is plain understanding, in-depth I will not, hehe.

Summary: The use of references is to reduce the consumption of memory resources.

The reference is explained in the PHP manual as follows:

Referencing in PHP means accessing the same variable content with a different name. This is not like the C pointer, instead, the reference is the symbol 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 filename and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be seen 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 the $a also changed, if the $b destroyed (the memory does not occupy space, not NULL, is not a value of ""), the value of $a will also be deleted after it?

In fact, there is a PHP in the foreign book specifically mentioned this mechanism. Seen 2 years ago. I don't remember. The principle is that when you delete a variable, it is automatically copied.
In fact, this is to avoid the deletion of $b, resulting in a $ A also to delete the problem.

<?php$a = ' d '; $b = & $a; $b = 8;//because it is a reference, so change the value of B and the value of a is 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?>

When calling unset to delete the $b variable, the PHP engine finds it from the variable symbol table: The variable i want to delete $b originally referred to the variable $ A, which is not good to delete ah, because the deletion caused a $ A variable is also gone, so we first copy a $ A variable after the deletion of the $b variable.

About the PHP symbol table: In fact, I understand that all variable names are recorded in the Run, PHP to maintain, the specific data is stored in memory, PHP is based on this symbol table to reclaim unused variable space, free memory space. Take a look at the garbage collection mechanism of PHP (freeing up memory space that is no longer used), 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 of the new variable, equivalent to $big_long_variable_name= "PHP"; */$short =& $big _long_variable_name; /* The value of the variable $big_long_variable_name is assigned to the variable $short, at which point the value of $short is "PHP", equivalent to $short=& $ $long;  */print "01/$short is $short."; /* "/$" is an escape sequence that represents the output of a dollar sign $, same as below. The purpose of this statement is output: $short is PHP. */print "Long is $big _long_variable_name."; /* Output:--Long is PHP. */?><br/><br/><?php $big _long_variable_name.= "rocks!"; /* Re-assign values to $big_long_variable_name. During the re-assignment process, the value of the variable $big_long_variable_name at this time should be the original value ("PHP") + New Value ("rocks!") due to the addition of a. (dot number) after $big_long_variable_name. ), that is, the current full value of the variable $big_long_variable_name is "PHP rocks!". Same below  */print "03/$short is $short"; /* Output: $short is PHP rocks! */print "Long is $big _long_variable_name"; /* Output: Long is PHP rocks! */?><br/><br/>05 $short is php rocks!06 Long is php rocks!<br/><br/><?php $short. = "Progr  Amming $short "; /* Re-assign the variable $short. Since the. (dot) is added after $short, please refer to the above example to analyze the value of $short. */print "07/$short is $short "; /* Output: $short is PHP rocks! Programming PHP rocks! */print "Long is $big _long_variable_name"; /* Because the variable $short is re-assigned to programming PHP rocks!, the value of the variable $big_long_variable_name is also changed with $short to "PHP rocks! Programming PHP rocks! ". This statement output: Long is PHP rocks! Programming PHP rocks! Note that if you destroy a variable with the same value unset (), the other variable does not apply to this situation, that is, it will not be destroyed along with it. */?><br/><br/>09 $short is programming PHP rocks!10 Long is programming php rocks!<br/><br/&gt ; <?php $big _long_variable_name.= "Web programming $short"; /* The variable $big_long_variable_name is re-assigned, at which point its full value should be PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks!. The variable $short is the same as the variable $big_long_variable_name. Please refer to the 5th and 10th notes respectively for analysis.   */print "11/$short is $short"; /* Output: PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks! */print "A Long is $big _long_variable_name";?  ><br/><br/><?phpunset ($big _long_variable_name); /* Destroy variable $big_long_variable_name with unset (), Variable $shoRT will not be affected as a result.  */print "13/$short is $short"; /* Although the variable $big_long_variable_name is destroyed, but $short is not affected, its value is still the most recently given PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks! */print "A Long is $big _long_variable_name."; /* Variable $big_long_variable_name has been destroyed, hence no value. Output: Long is.                  */snow;  ? ><br/><br/><?php $short = "No point TEST1"; /* Re-assign the variable $short. Because this time there is no add 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 at the back of the $short. (dot), but it refers to its own last value "no point TEST1".  */print "16/$short is $short."; /* Output: $short is no. TEST2 no point TEST1. */

The above is the whole content of this article, I hope that everyone's study has helped.


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.