Analysis of Variable Reference and Variable Destruction Mechanism in PHP

  • 2021-07-26 07:19:53
  • OfStack

In this paper, the mechanism of variable reference and variable destruction in PHP is analyzed with examples. Share it for your reference. The specific analysis is as follows:

Variable is a very important type in php. All our data are operated by variables or constants. Let's look at variable reference and variable destruction below.

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

1. Look at the non-reference situation like this:

$a  = "hello world";// Definition 1 Variable, assigned below to $b
$b = $a;// This 1 Step is not in $a Add a symbol before it & , like this "$b= & $a" . Without adding & In fact, the principle is that the variable $a Copy copy 1 Copies, that is, reapply in memory 1 Address storage variables $b It's over

ps: In php, using "=" to assign values directly is actually copying one variable on the right to b, which will generate one memory space, and the result may be two copies of the same content in memory. In some aspects of php performance, it is mentioned that this will occupy more memory space. However, in my contact, most people didn't pay much attention to it. In fact, the significant difference between 1 general application and this application is not obvious. I won't see any effect. In fact, I don't use it often. & Make a reference, hehe. It's just that I think it's very necessary to have a deep understanding of the implementation principle. I like to pay attention to the principle.

2. Use symbols & Make a reference

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

Using a reference, the php engine does not copy a variable, instead pointing the pointer to the in-memory address of $a, which is stored in $b.
So when using a reference, change the value of $b, and $a will change accordingly
For example:
$a  = "hello world";
$b = &$a;
$b = "test new value";// Put b Change the value of, a The value of will change accordingly
echo $a;// Output test new value Because it's changed b The value of will also change a The value of.

Often you see things like this when you define a function:

function test (& $param)
{
// Content of function definition
$param++;
}

Explanation: $param is preceded by a reference, so the incoming parameters will not be copied in memory, but directly refer to the original memory space. So: If you use symbols in the & If the value of the variable passed in is modified, the value in the original memory space will also be changed.
Take a test as follows:
$k = 8;
test($k);
echo $k;// Results $k The value of is changed by the function, and the output is 9 .

You will often see functions called like this:
$return  = & test_func();

I learned earlier that the mechanism of php engine is: = will copy the content on the right and give it to the variable on the left. So use & That is, the result of the function will not be copied. In fact, my understanding is to give the pointer to the variable on the left.
What is a pointer? I used to learn the concepts in c language. My understanding is: pointer, pointer, pointing needle (compass, hehe). Think of the pointer as a memory address easy to understand point, the computer will know where to find data in memory. This is a simple understanding, but I won't go deep, hehe.

Summary: References are used to reduce the occupation of memory resources.

The reference in the php manual is explained as follows:

Reference in PHP means accessing the contents of the same variable with different names. This is not like the pointer of C. Instead, the reference is a symbol table alias. Note that in PHP, variable names and variable content are not identical, 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 thought of as hardlink in the Unix file system.

3. When destroying variables. Does not change the original value.

Trial: $b = & $a;
Since the value of $b is changed, the value of $a is also changed. If $b is destroyed (no space is occupied in memory, not null, nor is the value ""), will the value of $a be deleted?

In fact, there is a foreign book on php that specifically mentions this mechanism. I saw it two years ago. I don't remember very much. The principle is that when a variable is deleted, it will be copied automatically.

In fact, this is to avoid deleting $b, resulting in deleting $a.

<?php
$a = 'd';
$b = & $a;
$b = 8;// Because it is quoted, so put b Change the value of, a The value of is also changed to 8 It's over.
var_dump($b,$a);
unset($b);// Call unset Delete b Variables, a Variables are not deleted
var_dump($b,$a);// Output null And 8

When calling unset to delete the $b variable, php engine found from the variable symbol table that the variable $b I want to delete originally referenced the variable $a, which is not easy to delete, because 1 deletion leads to the loss of the $a variable, so first copy the $a variable and then delete the $b variable.
About php symbol table: In fact, my understanding is that all variable names are recorded in it during operation, and php maintains them. Of course, the specific data is stored in memory. php recycles unused variable space according to this symbol table and releases memory space. Take a look at the garbage collection mechanism of php (releasing memory space that is no longer used), which is based on the symbol table.
Example
<?php
$long="big_long_variable_name";
$$long="PHP";     /* 用存放在变量$long里的字符串作为新变量的变量名,等同于$big_long_variable_name="PHP"; */
$short=& $big_long_variable_name;  /* 取变量$big_long_variable_name的值赋给变量$short,此时$short的值为"PHP",等同于$short=& $$long; */
print "01 /$short is $short.";   /* "/$"是转义序列,表示输出1个美元符号$,下同。本语句的作用是输出:01 $short is PHP. */
print "02 Long is $big_long_variable_name."; /* 输出:02 Long is PHP. */
?>
<br />
<br />
<?php
$big_long_variable_name.=" rocks!"; /* 重新对$big_long_variable_name赋值。重新赋值过程中,由于在$big_long_variable_name的后面添加了.(点号),因而变量$big_long_variable_name此时的值应为原值("PHP")+新值(" rocks!"),即变量$big_long_variable_name当前完整的值为"PHP rocks!"。下同。*/
print "03 /$short is $short";   /* 输出:03 $short is PHP rocks! */
print "04 Long is $big_long_variable_name"; /* 输出:04 Long is PHP rocks! */
?>
<br />
<br />
05 $short is PHP rocks!
06 Long is PHP rocks!
<br />
<br />
<?php
$short.="Programming $short";   /* 重新对变量$short赋值。由于在$short后面添加了.(点号),因此请参考上例分析$short的值。*/
print "07 /$short is $short";   /* 输出:07 $short is PHP rocks!Programming PHP rocks! */
print "08 Long is $big_long_variable_name"; /* 由于变量$short被重新赋值为Programming PHP rocks!,因而变量$big_long_variable_name的值也与$short1同被改变为"PHP rocks!Programming PHP rocks!"。本语句输出:08 Long is PHP rocks!Programming PHP rocks!注意,如果是对具有相同值的1个变量进行销毁unset( ),则另1个变量不适用于此种情况,即不会随之被1同销毁。*/
?>
<br />
<br />
09 $short is Programming PHP rocks!
10 Long is Programming PHP rocks!
<br />
<br />
<?php
$big_long_variable_name.="Web Programming $short";  /* 变量$big_long_variable_name被重新赋值,此时它完整的值应为PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks!。变量$short的值此时与变量$big_long_variable_name1致。请分别参考第5处、第10处注释进行分析。*/
print "11 /$short is $short";     /* 输出:11 PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks! */
print "12 Long is $big_long_variable_name";
?>
<br />
<br />
<?php
unset($big_long_variable_name);   /* 用unset( )销毁变量$big_long_variable_name,变量$short不会因此受到任何影响。*/
print "13 /$short is $short";   /* 虽然销毁了变量$big_long_variable_name,但$short没有受到影响,它的值仍是最近1次被赋予的PHP rocks!Programming PHP rocks!Web Programming PHP rocks!Programming PHP rocks! */
print "14 Long is $big_long_variable_name."; /* 变量$big_long_variable_name已被销毁,故而无值。输出:14 Long is. */
snow;                                   
?>
<br />
<br />
<?php $short="No point TEST1";   /* 重新对变量$short赋值。由于这次没有在$short后面添加.(点号),因此$short当前的值为"No point TEST1"。*/
print "15 /$short is $short.";   /* 输出:15 $short is No point TEST1. */
$short="No point TEST2 $short";   /* 重新对变量$short赋值。没在$short的后面添加.(点号),但引用了它自身最近1次的值"No point TEST1"。*/
print "16 /$short is $short.";   /* 输出:16 $short is No point TEST2 No point TEST1. */

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


Related articles: