Home  >  Article  >  Backend Development  >  Analysis of variable reference and variable destruction mechanism in PHP, PHP destruction_PHP tutorial

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

WBOY
WBOYOriginal
2016-07-13 10:13:591070browse

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

This article analyzes the variable reference and variable destruction mechanism in PHP with examples. Share it with everyone for your reference. The specific analysis is as follows:

Variables are a very important type in PHP. All our data are operated through variables or constants. Let’s take a look at variable references and variable destruction below.

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

1. Look at the situation without citation:

Copy code The code is as follows:
$a = "hello world";//Define a variable and assign it to $b
$b = $a;//This step does not add the symbol & before $a, like this "$b= & $a". Without adding &, the actual principle is to make a copy of the variable $a, that is, a new address is applied to store the variable $b in the memory

ps: In PHP, using "=" to assign a value directly is actually copying a copy of the variable on the right to b, which will generate a memory space. As a result, the same content may be stored in two copies in the memory. As mentioned in some aspects about PHP performance, this will occupy more memory space. However, in my contacts, most people didn't pay much attention to it. In fact, the significant difference caused by general application in this way is not obvious. You won't see any effect. In fact, I don't often use & for citations, haha. It’s just that I think it is very necessary to have a deep understanding of the implementation principles. I like to focus on fundamental things.

2. Use the symbol & for citation

Copy code The code is as follows:
$a = "hello world";
$b = &$a;

Using references, the PHP engine does not copy a variable. In fact, it points the pointer to the address of $a in memory, and $b stores this pointer.
So when using references, if you change the value of $b, $a will also change accordingly
For example:
Copy code The code is as follows:
$a = "hello world";
$b = &$a;
$b = "test new value";//Change the value of b, and the value of a will also change accordingly
echo $a;//Output test new value, because changing the value of b will also change the value of a.

I often see situations like this when defining functions:

Copy code The code is as follows:
function test (& $param)
{
//Content of function definition
$param++;
}

Explanation: There is a reference in front of $param, so the parameters passed in will not be copied in the memory, but will directly reference the original memory space. So: If the variable value passed in using the symbol & is modified, the value in the original memory space will also be changed.
Take a quiz below:
Copy code The code is as follows:
$k = 8;
test($k);
echo $k;//As a result, the value of $k was changed in the function, and 9 is output.

You will often see functions called like this:
Copy code The code is as follows:
$return = & test_func();

I learned earlier that the mechanism of the PHP engine is: = will copy the content on the right to the variable on the left. So using & means copying the result of the function. In fact, my understanding is that the pointer is given to the variable on the left.
What is a pointer? I used to learn the concept in C language. My understanding is: pointer, pointer, pointing needle (compass, haha). It is easier to understand if you think of a pointer as a memory address. The computer will know where to find data in the memory. This is a superficial understanding, I don’t know how to understand it in depth, haha.

Summary: Using references is to reduce memory resource usage.

The php manual explains the reference as follows:

Quoting in PHP means accessing the same variable content with different names. This is not like a C pointer; instead, the reference is a symbol table alias. Note that in PHP, variable names and variable contents are different, so the same content can have different names. The closest analogy is Unix's filenames and the files themselves - the variable names are the directory entries, and the variable contents are the files themselves. References can be thought of as hardlinks in Unix file systems.

3. When destroying variables. It will not change the original value.

Test: $b = & $a;
Since the value of $b is changed, the value of $a also changes. If $b is destroyed (no space is occupied in the memory, it is not null, and the value is ""), will the value of $a also be destroyed? Delete it?

In fact, there is a foreign book on PHP that specifically mentions this mechanism. Saw it 2 years ago. I don’t quite remember. The principle is that when a variable is deleted, it will be automatically copied.

In fact, this is to avoid deleting $b and causing the problem of deleting $a.

Copy code The code is as follows:
$a = 'd';
$b = & $a;
$b = 8;//Because it is quoted, the value of b is changed, and the value of a is also changed to 8.
var_dump($b,$a);
unset($b);//Call unset to delete variable b, variable a will not be deleted
var_dump($b,$a);//Output null and 8

When calling unset to delete the $b variable, the PHP engine found out from the variable symbol table: the variable $b I want to delete originally refers to the variable $a, which is difficult to delete because once deleted, the $a variable is gone. So first make a copy of the $a variable and then delete the $b variable.
Regarding the PHP symbol table: In fact, my understanding is that all variable names during operation are recorded in it and PHP maintains it. The specific data is of course stored in the memory. PHP uses this symbol table to recycle unused variable space. , free up memory space). Go take a look at PHP's garbage collection mechanism (release memory space that is no longer used), which is based on the symbol table.
Example
Copy code The code is as follows:
$long="big_long_variable_name";
$$long="PHP"; /* Use the string stored in the variable $long as the variable name of the new variable, which is equivalent to $big_long_variable_name="PHP"; */
$short=& $big_long_variable_name; /* Take the value of variable $big_long_variable_name and assign it to variable $short. At this time, the value of $short is "PHP", which is equivalent to $short=& $$long; */
print "01 /$short is $short."; /* "/$" is an escape sequence, indicating that a dollar sign $ is output, the same below. The function of this statement is to output: 01 $short is PHP. */
print "02 Long is $big_long_variable_name."; /* Output: 02 Long is PHP. */
?>




$big_long_variable_name.=" rocks!"; /* Reassign $big_long_variable_name. During the reassignment process, since a . (dot) is added after $big_long_variable_name, the value of the 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!". The same below. */
print "03 /$short is $short"; /* Output: 03 $short is PHP rocks! */
print "04 Long is $big_long_variable_name"; /* Output: 04 Long is PHP rocks! */
?>




05 $short is PHP rocks!
06 Long is PHP rocks!




$short.="Programming $short"; /* Reassign the variable $short. Since . (dot) is added after $short, please refer to the above example to analyze the value of $short. */
print "07 /$short is $short"; /* Output:07 $short is PHP rocks! Programming PHP rocks! */
print "08 Long is $big_long_variable_name"; /* Since the variable $short is reassigned to Programming PHP rocks!, the value of the variable $big_long_variable_name is also changed to "PHP rocks! Programming PHP rocks!" together with $short. This statement output: 08 Long is PHP rocks! Programming PHP rocks! Note that if a variable with the same value is destroyed by unset(), the other variable does not apply to this situation, that is, it will not be destroyed together. . */
?>




09 $short is Programming PHP rocks!
10 Long is Programming PHP rocks!




$big_long_variable_name.="Web Programming $short"; /* The variable $big_long_variable_name is reassigned. At this time, its complete value should be PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks!. The value of variable $short is now consistent with variable $big_long_variable_name. Please refer to notes 5 and 10 respectively for analysis. */
print "11 /$short is $short"; /* Output:11 PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks! */
print "12 Long is $big_long_variable_name";
?>




unset($big_long_variable_name); /* Use unset() to destroy the variable $big_long_variable_name. The variable $short will not be affected in any way. */
print "13 /$short is $short"; /* Although the variable $big_long_variable_name is destroyed, $short is not affected, and its value is still the last assigned value PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks! */
print "14 Long is $big_long_variable_name."; /* The variable $big_long_variable_name has been destroyed, so it has no value. Output: 14 Long is. */
snow;                                         ?>




print "15 /$short is $short."; /* Output: 15 $short is No point TEST1. */
$short="No point TEST2 $short"; /* Reassign the variable $short. No . (dot) is added after $short, but its latest value "No point TEST1" is quoted. */
print "16 /$short is $short."; /* Output:16 $short is No point TEST2 No point TEST1. */

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/912291.htmlTechArticleAnalysis of variable reference and variable destruction mechanism in PHP, php destruction This article analyzes the variable reference and variable destruction mechanism in PHP with examples . Share it with everyone for your reference. The specific analysis is as follows: Variables...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn