Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle of variable reference and variable destruction Mechanism in PHP

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the principle of variable reference and variable destruction mechanism in PHP". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the principle of variable reference and variable destruction mechanism in PHP.

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

1. Take a look at the situation that is not quoted as follows:

The copy code is as follows:

$a = "hello world"; / / define a variable and assign the following value to $b

/ this step does not precede $a with a sign &, like "$b = & $a". Without adding &, in fact, the principle is to make a copy of the variable $a, that is, to re-apply for an address storage variable $b in memory.

Ps: in php, using "=" to directly assign a value is actually a copy of the variable on the right to b, which will generate a memory space, and the result may be two copies of the same content in memory. In some aspects of php performance, this takes up more memory space. However, when I came into contact, most people didn't pay much attention to it. In fact, the significant difference in general application is not obvious. Will not see any effect, in fact, I do not often use & to quote, hehe. It's just that I think it's very necessary to have an in-depth understanding of the implementation principle. I like to focus on the principle.

2. Use symbols & to make references

The copy code is as follows:

$a = "hello world"

$b = & $a

With references, the php engine does not copy a variable, but simply points the pointer to the address of $an in memory, which is saved in $b.

So when using a reference, change the value of $b, and $a will change as well.

For example:

The copy code is as follows:

$a = "hello world"

$b = & $a

B = "test new value"; / / change the value of b, and the value of a will change accordingly.

Echo $a / output test new value, because changing the value of b will also change the value of a.

You often see situations like this when defining functions:

The copy code is as follows:

Function test ($param)

{

/ / the content of the function definition

$param++

}

Explanation: $param is preceded by a reference, so the passed-in parameters are not copied in memory, but refer directly to the original memory space. So: if the value of the variable passed in by using the symbol is modified, it will also change the value in the original memory space.

Take a test as follows:

The copy code is as follows:

$k = 8

Test ($k)

The value of echo $k / result $k is changed by the function, outputting 9.

You will also often see functions called like this:

The copy code is as follows:

$return = & test_func ()

We learned that the mechanism of the php engine is: = will make a copy of the content on the right and give it to the variable on the left. So to use & is to make a copy of the result of the function. 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 the c language. My understanding is: pointer, pointer, compass (ha ha). If you think of the pointer as an easy-to-understand point of the address of memory, the computer will know where to look for data in memory. This is a simple understanding, in-depth I will not, hehe.

Summary: use references to reduce the footprint of memory resources.

The citations in the php manual are explained as follows:

Referencing in PHP means accessing the contents of the same variable with a different name. This is not like a pointer to C, instead, the reference is a symbolic table alias. Note that in PHP, the variable name is different from the variable content, so the same content can have different names. The closest analogy is the file name of Unix 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.

Test: $b = & $a

Since the value of $b is changed, the value of $an is also changed. if $b is destroyed (no space in memory, not null, and the value is not ""), will the value of $a be deleted as well?

In fact, this mechanism is specifically mentioned in a foreign book on php. I saw it two years ago. I don't really remember. The principle is that when you delete a variable, it will be copied automatically.

In fact, this is to avoid deleting $b and causing the problem of deleting $an as well.

The copy code is as follows:

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report