Analysis of variable reference and variable destruction mechanism in PHP, PHP destruction _php Tutorial

Source: Internet
Author: User
Tags php programming

Analysis of variable reference and variable destruction mechanism in PHP, PHP destruction


This paper analyzes the variable reference and the mechanism of variable destruction in PHP. Share to everyone for your reference. The specific analysis is as follows:

Variables are a very important type in PHP, and our data is manipulated by variables or constants, and the following is a look at variable references and variable destruction.

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

1. Look at the non-quoted situation is this:
Copy the code as follows: $a = "Hello world";//define a variable and 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
Copy the code as follows: $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:
Copy the code as follows: $a = "Hello World";
$b = & $a;
$b = "test new value";//change the value of B, and 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 the Code code as follows: Function test (& $param)
{
What the function defines
$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:
Copy the Code code 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 a calling function:
Copy the Code code as follows: $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.
Copy CodeThe code is as follows: <?php
$a = ' d ';
$b = & $a;
$b = 8;//because it is a reference, so the value of B is changed, the value of A is followed by 8.
Var_dump ($b, $a);
Unset ($b);//Call unset to delete the b variable, a variable is not deleted
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
Copy CodeThe code is as follows: <?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. */
?>




<?php
$big _long_variable_name.= "rocks!"; /* Re-assign values to $big_long_variable_name. During the re-assignment process, it was added after the $big_long_variable_name. (dot), so the variable $big_long_variable_name value at this time should be the original value ("PHP") + New Value ("rocks!"), that is, the variable $big_long_variable_name the current full value 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! */
?>




$short is PHP rocks!
Long is PHP rocks!




<?php
$short. = "Programming $short"; /* Re-assign the variable $short. Because it was added after $short. (dot), so 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. */
?>




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




<?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";
?>




<?php
unset ($big _long_variable_name); /* Destroy the variable $big_long_variable_name with unset (), and the variable $short will not be affected by this. */
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
?>




<?php $short = "No point TEST1"; /* Re-assign the variable $short. Because this time is not added behind the $short. (dot number), 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 in 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. */

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/912291.html www.bkjia.com true http://www.bkjia.com/PHPjc/912291.html techarticle analysis of variable reference and variable destruction mechanism in PHP, PHP destruction This paper analyzes the variable reference and the mechanism of variable destruction in PHP. Share to everyone for your reference. The specific analysis is as follows: Variable ...

  • 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.