PHP reference passing
Although I am also a learner of PHP, I really didn’t know much about the garbage collection process inside PHP before. We just used unset, null, mysql_close, __destruct and other functions in our code to release objects to prevent memory overflow, so I found the following instructions under GG on the Internet and made a record: "PHP can automatically manage memory and clear objects that are no longer needed. PHP uses a simple garbage collection mechanism called reference counting. Every time Each object contains a reference counter, and each reference is connected to the object. When the reference leaves the living space or is set to NULL, the counter is decremented by 1. When the reference counter of an object is zero, PHP knows that you will. This object is no longer needed and the memory space it occupies is released.”
As we all know, the PHP engine itself is written in C. When mentioning C, we must mention GC (garbage collection). Through the PHP manual, we know that the PHP engine will automatically perform GC actions. So we can’t help but ask, what exactly does it do? How is it recycled? Is the & reference operation a pointer? When unset() is used to unset() a variable, is it really recycled? These seem to be issues mentioned in the manual, but if you analyze them carefully, you will find that they are far from simple and general. . Maybe someone will jump out and say: You can know it by looking at the PHP source code. Yes, after you read through the PHP source code, this problem will definitely go away. However, this article will only analyze these seemingly ordinary but commonly used problems from PHP itself. I have overlooked the small details. Of course, it is inevitable that my level is limited and there are some omissions. I warmly welcome everyone.
phper to discuss together.
First of all, let’s see the example, the simplest execution process:
Example 1: gc.php
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
echo $b ." ";
?>
Needless to say% php -f gc.php The output result is very clear:
hy0kl% php -f gc.php
I am test.
OK, next:
Example 2:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = 'I will change?';
echo $a ." ";
echo $b ." ";
?>
The execution result is still obvious:
hy0kl% php -f gc.php
I will change?
I will change?
Please take a look :
Example 3:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
unset($a);
echo $a ." ";
echo $b ." ";
?>
Do you have to think about it?
hy0kl% php -f gc.php
Notice: Undefined variable: a in /usr/local/www/apache22/data/test /gc.php on line 8
I am test.
Are you a little confused?
Look again:
Example 4:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a ;
unset($b);
echo $a ." ";
echo $b ." ";
?>
In fact, if you understand Example 3, this is the same as Example 3.
hy0kl% php -f gc.php
I am test.
Notice: Undefined variable: b in /usr/local/www/apache22/data/test/gc.php on line 9
Look at:
Example 5:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$a = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
What is the first feeling of fierceness?
hy0kl% php -f gc.php
$a =
$b =
Yes, this is the output result. PHPers who have a deep understanding of PHP GC will not find it strange. To be honest, I was surprised when I ran this code for the first time, but it made me wonder. Now that you have a deeper understanding of PHP GC, the following example of working with it will naturally be easier to understand.
Example 6:
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
?>
OK, if the result of the above example does not have any details for the viewer, then you can close this window. Welcome to come back when you have time!
Let’s analyze GC and references in detail.
1. In all examples, a variable is created. In layman’s terms, this process is to open up a space in the memory and store a string I in it. am test.. There is a symbol table inside PHP to record the reference count of each block of memory. At this time, the reference count of this block of memory will be increased by 1, and a label (variable) named $a will point to this block of memory. , which makes it easy to operate the memory according to the tag name.
2. Perform & operation on variable $a. My understanding is to find the memory pointed by $a, establish the same reference point for $b, and store the memory block of the string I am test. in the symbol table. The reference count is increased by 1. In other words, when our script executes this line, the memory storing the string I am test. is referenced twice. What should be emphasized here is that the & operation establishes a reference point, not Pointers, PHP has no concept of pointers! At the same time, some people have proposed file soft links similar to UNIX. It can be understood to a certain extent: the memory storing the character I am test. is a real file of ours, and the variable $a and
$b is a soft link established for a real file, but they point to the same real file. So, we see that when $b is assigned a value in Example 2, the value of $a also changes. And through a certain A soft link operates the file similarly.
3. In Examples 3 and 4, the unset() operation was performed. According to the actual execution results, it can be seen that: unset() only disconnects the reference of the variable to the memory it originally pointed to, making the variable itself undefined. If a null reference is passed, a Notice is issued when the call is made, and the reference count of that piece of memory in the symbol table is decremented by 1, and it does not affect other variables pointing to this piece of memory. In other words, only when a piece of memory has a reference count in the symbol table When it is 0, the PHP engine will reclaim this memory.
PHP Manual
4.0.0 unset() became an expression. (In PHP 3, unset() would always return 1).
What does this mean?
Look at the following code and its result:
error_reporting (E_ALL);
$a = 'I am test.';
$b = & $a;
unset($a);
unset($a);
unset($a);
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
? >
hy0kl% php -f gc.php
Notice: Undefined variable: a in /usr/local/www/apache22/data/test/gc.php on line 10
$a =
$b = I am test.
The first unset() operation has disconnected the pointer, so subsequent operations will not affect the reference count of any memory in the symbol table.
4. Through Example 5 & 6, it can be clearly concluded that the operation of assigning null is quite violent. It will directly set the reference count in the symbol number of the memory pointed to by the variable to 0. Then this memory will naturally be reclaimed by the engine. As for how It is unknown when it will be used again. It may be used to store other information immediately, and may never be used again. But in any case, all the original variables pointing to that memory will no longer be able to operate the reclaimed memory. Any attempt to Any variable calling it will return null.
error_reporting(E_ALL);
$a = 'I am test.';
$b = & $a;
$b = null;
echo '$a = '. $a ." ";
echo '$b = '. $b ." ";
if (null === $a)
{
echo '$a is null.';
} else
{
echo 'The type of $a is unknown.';
}
?>
hy0kl% php -f gc.php
$a =
$b =
$a is null.
In summary, it fully explains why we are looking at the source code of open source products Sometimes, we often see that some relatively large temporary variables, or reused information that is no longer called after use, will be concentrated or displayed as null. It is equivalent to directly killing the real file in UNIX, and all soft links pointing to it Naturally it became an empty chain
The above introduces the reference passing of PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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.
