In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "what is PHP variable separation". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is PHP variable separation"?
First, let's review the structure of zval:
The copy code is as follows:
Struct _ zval_struct {
/ * Variable information * /
Zvalue_value value; / * value * /
Zend_uint refcount
Zend_uchar type; / * active type * /
Zend_uchar is_ref
}
The refcount and is_ref fields have never been introduced, and we know that PHP is a long-running server-side script interpreter. So for it, efficiency and resource utilization are a very important measure, that is, PHP must introduce memory utilization as much as possible, considering the following code:
The copy code is as follows:
The first line of code creates a string variable, requests a 9-byte memory, and holds the end of the string "laruence" and a NULL (/ 0).
The second line defines a new string variable and "copies" the value of the variable var to the new variable.
The third line unset contains the variable var
This kind of code is very common in our usual scripts. If PHP reallocates memory and copy data for each variable assignment, then the above code has to apply for 18 bytes of memory space, and we can easily see that there is no need to apply for two spaces at all. Hehe, PHP developers can also see that:
As we said before, variables in PHP are implemented with a symbolic name stored in symbol_table, corresponding to a zval. For example, for the first line of code above, a value "var" is stored in symbol_table, corresponding to a pointer to a zval structure, and the variable value "laruence" is stored in this zval, so it is not difficult to imagine that for the above code, We can just make the pointers corresponding to "var" and "var_dup" point to the same zval.
PHP does the same thing, and it's time to introduce the refcount field in the zval structure that we haven't covered before.
Refcount, as its name implies, records the number of references to the current zval.
For example, for code:
The copy code is as follows:
In the first line, you create an integer variable with a value of 1. At this point, the refcount of the zval that saves shaping 1 is 1.
In the second line, you create a new integer variable, which also points to the zval you just created, and add 1 to the refcount of the zval, so that the refcount of the zval is 2.
PHP provides a function to help us understand the process debug_zval_dump:
The copy code is as follows:
Output:
Long (1) refcount (2)
Long (1) refcount (3
If you're surprised, var's refcount should be 1?
We know that for simple variables, PHP passes through parameters in the form of passing values. That is, when you execute debug_zval_dump ($var), $var is passed to debug_zval_dump as a value, which causes var's refcount to add 1, so we just have to see the fact that when a variable is assigned to a variable, it causes zval's refcount to add 1.
Now let's go back to the code at the beginning of the article. What happens when the last line of unset ($var) is executed? Yes, since refcount minus 1, the code above:
The copy code is as follows:
Output:
String (8) "laruence" refcount (2
But what about the following code?
The copy code is as follows:
Obviously, after this code is executed, the value of $var_dup should still be "laruence", so how is this achieved?
This is the copy on write mechanism of PHP:
Before modifying a variable, PHP will first check the variable's refcount. If refcount is greater than 1, PHP will execute a separate routine. For the above code, when executing to the third line, PHP finds that the refcount of $var pointing to zval is greater than 1, then PHP will copy a new zval, subtract 1 from the original zval's refcount, and modify symbol_table to make $var and $var_dup separate (Separation). This mechanism is called copy on write (copy on write).
Code testing on:
The copy code is as follows:
Output:
Long (1) refcount (2)
String (8) "laruence" refcount (2
Now we know that when using variable replication, PHP is not really replicated internally, but uses the same structure to point to the same structure to minimize overhead. So how do you implement references in PHP?
The copy code is as follows:
At the end of this code, $var is also indirectly modified to 1, a process called (change on write: change on write). So how does ZE know that Separation is not needed for this replication?
This is the time to use the is_ref field in zval:
For the above code, when the second line is executed, the refcount of the zval represented by $var becomes 2 and is_ref is set to 1 at the same time.
On the third line, PHP first checks the is_ref field of the zval represented by var_ref. If it is 1, it will not be separated. The general logic is as follows:
The copy code is as follows:
If ((* val)-> is_ref | | (* val)-> refcount
Output:
String (8) "laruence" refcount (1
At this point, I believe you have a deeper understanding of "what is PHP variable separation". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.