Analysis of variable reference and destruction mechanism in PHP, and destruction of php. Analysis of variable reference and destruction mechanism in PHP. php destruction examples in this article analyze the variable reference and destruction mechanism in PHP. Share it with you for your reference. The specific analysis is as follows: variable reference and destruction mechanism analysis in variable PHP, and php destruction
This article analyzes the variables reference and destruction mechanisms in PHP. Share it with you for your reference. The specific analysis is as follows:
Variables are a very important type in php. all of our data is operated by variables or constants. let's take a look at variable references and destruction.
In php, the symbol "&" indicates reference.
1. check whether the data is not referenced in this way:
The code is as follows:
$ A = "hello world"; // defines a variable, which is assigned to $ B.
$ B = $ a; // This step is not signed before $ a, like this "$ B = & $ ". Without adding &, the principle is to copy the variable $ a, that is, re-apply for an address storage variable $ B in the memory.
Ps: In php, using "=" to directly assign values is actually copying a copy of the right variable to B and generating a memory space, the result may be two copies of the same content in the memory. As mentioned in some aspects about php performance, this will occupy more memory space. However, most people do not pay much attention to it. In fact, there is no significant difference in the use of such applications. I don't see any results. In fact, I don't often use and reference them. I think it is necessary to have a deep understanding of the implementation principles. I like principles.
2. use symbols for reference
The code is as follows:
$ A = "hello world ";
$ B = & $;
With reference, the php engine does not copy a variable. In fact, it points the pointer to the address of $ a in the memory, and $ B saves the pointer.
So when using the reference, change the value of $ B and $.
For example:
The code is as follows:
$ A = "hello world ";
$ B = & $;
$ B = "test new value"; // change the value of B, and the value of a will also change.
Echo $ a; // output test new value, because changing the value of B also changes the value of.
When defining a function, you often see a situation like this:
The code is as follows:
Function test (& $ param)
{
// Function definition content
$ Param ++;
}
Explanation: $ param carries a reference before it. Therefore, the input parameters do not copy a copy in the memory, but directly reference the original memory space. Therefore, if you modify the variable value with the symbol & input, the value in the original memory space will also be changed.
Perform the following test:
The code is as follows:
$ K = 8;
Test ($ k );
Echo $ k; // The result $ k value is changed in the function and output 9.
You will often see this call function:
The code is as follows:
$ Return = & test_func ();
As mentioned above, the php engine mechanism is as follows: = will copy the content on the right to the left of the variable. So using & means that the result of the function will not be copied. 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 of C language. My understanding is: Pointer, pointer, pointing to the needle (compass, haha ). It is easy to understand the pointer as a memory address. the computer will know where to find data in the memory. This is a simple understanding. I won't go into depth, huh, huh.
Conclusion: reference is used to reduce memory resource usage.
The reference in the php Manual is explained as follows:
Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. A reference can be considered as a hardlink in a Unix file system.
3. destroy variables. It does not change the original value.
Test: $ B = & $;
Since the value of $ B is changed, the value of $ a is also changed. if $ B is destroyed (the memory does not occupy space, it is not null or the value is ""), will the value of $ a be deleted?
In fact, this mechanism has been specifically mentioned in a php book abroad. I saw it two years ago. Not very remembered. The principle is that variables are automatically copied when they are deleted.
In fact, this is to avoid deleting $ B, resulting in the deletion of $.
The code is as follows:
<? Php
$ A = 'd ';
$ B = & $;
$ B = 8; // The value of B is changed to 8 because it is referenced.
Var_dump ($ B, $ );
Unset ($ B); // call unset to delete variable B. variable a is not deleted.
Var_dump ($ B, $ a); // outputs null and 8
When unset is called to delete the $ B variable, the php engine found from the variable symbol table that the variable $ B I want to delete was originally referenced by the variable $ a, which is difficult to delete, because the $ a variable is deleted, copy the $ a variable and delete it.
Php symbol table: In fact, I understand that all variable names are recorded in the runtime and maintained by php. the specific data is certainly stored in the memory, php recycles unused variable space based on the symbol table and releases the memory space ). Let's take a look at php's garbage collection mechanism (releasing memory space that is no longer in use), which is based on the symbol table.
Example
The code is as follows:
<? Php
$ Long = "big_long_variable_name ";
$ Long = "PHP";/* use the string stored in the variable $ long as the name of the new variable, equivalent to $ big_long_variable_name = "PHP ";*/
$ Short = & $ big_long_variable_name;/* assign the value of $ big_long_variable_name to the variable $ short. The value of $ short is "PHP ", equivalent to $ short = & $ long ;*/
Print "01/$ short is $ short.";/* "/$" is an escape sequence, indicating to output a dollar sign $, the same below. The purpose of this statement is to output: 01 $ short is PHP .*/
Print "02 Long is $ big_long_variable_name.";/* output: 02 Long is PHP .*/
?>
<? Php
$ Big_long_variable_name. = "rocks! ";/* Assign a value to $ big_long_variable_name again. During the value re-assignment process, because the. (point number) is added after $ big_long_variable_name, the value of the variable $ big_long_variable_name should be the original value ("PHP") + new value ("rocks! "), That is, the current complete value of the variable $ big_long_variable_name 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!
<? Php
$ Short. = "Programming $ short";/* value the variable $ short again. Because. (dot) is added to the end of $ 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";/* the variable $ short is assigned a new value to Programming PHP rocks !, Therefore, the value of $ big_long_variable_name is changed to "PHP rocks!" together with $ short! Programming PHP rocks! ". Output of this statement: 08 Long is PHP rocks! Programming PHP rocks! Note: If you destroy unset () for a variable with the same value, the other variable is not applicable to this situation and will not be destroyed together. */
?>
09 $ short is Programming PHP rocks!
10 Long is Programming PHP rocks!
<? Php
$ Big_long_variable_name. = "Web Programming $ short";/* the variable $ big_long_variable_name is assigned a new value, and the complete value should be PHP rocks! Programming PHP rocks! Web Programming PHP rocks! Programming PHP rocks !. The value of $ short is consistent with $ big_long_variable_name. For analysis, see comments in section 5th and section 10th. */
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 ";
?>
<? Php
Unset ($ big_long_variable_name);/* use unset () to destroy the variable $ big_long_variable_name. the variable $ short will not be affected. */
Print "13/$ short is $ short";/* Although the variable $ big_long_variable_name is destroyed, $ short is not affected and its value is still the PHP rocks that was last granted! 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 and therefore has no value. Output: 14 Long is .*/
Snow;
?>
<? Php $ short = "No point TEST1";/* value the variable $ short again. Because No. is added after $ short this time, the current value of $ short is "No point TEST1 ". */
Print "15/$ short is $ short.";/* output: 15 $ short is No point TEST1 .*/
$ Short = "No point TEST2 $ short";/* value the variable $ short again. No. is added after $ short, but its last value "No point TEST1" is referenced ". */
Print "16/$ short is $ short.";/* output: 16 $ short is No point TEST2 No point TEST1 .*/
I hope this article will help you with php programming.
In this article, we analyze the variable reference and destruction mechanisms in PHP. Share it with you for your reference. The specific analysis is as follows: variable...