


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
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:
$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
$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:
$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:
{
//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:
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:
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.
$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
$$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.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor