Home > Article > Backend Development > PHP reference & detailed explanation_PHP tutorial
A simple ampersand in PHP can lead to a large article. Today we will briefly talk about the usage of variable references and parameter value transfer in PHP. I hope beginners will read and refer to this article.
What $a=test(); gets in this way is not actually a function reference return. It is no different from an ordinary function call. As for the reason: This is the regulation of PHP
PHP stipulates that what is obtained through $a=&test(); is the reference return of the function
As for what is a reference return (the PHP manual says: Reference return is used when you want to use a function to find which variable the reference should be bound to.) This nonsense made me unable to understand it for a long time
Using the above example to explain it is
Calling a function using $a=test() only assigns the value of the function to $a, and any changes to $a will not affect $b
in the function.
When calling a function through $a=&test(), its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place
That is to say, the effect equivalent to this is produced ($a=&b;), so changing the value of $a also changes the value of $b, so after executing
The code is as follows | Copy code | ||||
$a=&test();
|
Static variables are used here to let everyone understand the reference return of functions. In fact, the reference return of functions is mostly used in objects
Object reference
The above code is the effect of running in PHP5
In PHP5, object copying is achieved through references. In the above column, $b=new a; $c=$b; is actually equivalent to $b=new a; $c=&$b;
The role of quotation
Unquote
代码如下 | 复制代码 |
$a = 1; |
The code is as follows | Copy code | ||||
$a = 1;
$b =& $a;
|
The code is as follows | Copy code |
$var =& $GLOBALS["var"];<🎜> ?> |
This means that, for example, unset $var will not unset a global variable.
$this
In an object method, $this is always a reference to the object that calls it.
//Another little interlude below
The address pointing (similar to a pointer) function in PHP is not implemented by the user himself, but is implemented by the Zend core. The reference in PHP adopts the principle of "copy-on-write", which means that unless a write operation occurs, it points to the same address. Variables or objects will not be copied.
In layman terms
1: If there is the following code
In fact, at this time, $a and $b both point to the same memory address, rather than $a and $b occupying different memories
The code is as follows
|
Copy code
|
||||
$source="110"; $a=$source;$b=&$source; $source="120";echo $a."rn",$b;
This is a problem with PHP’s reference operator &. Because & is applied to variable $b when assigning value, $b does not copy “110” to itself but directly points to the nest of $source. From now on, $ The source is his $b. No matter how $source changes, it will lead to changes in $b - much like the relationship between a host and two monitors. Since this is the relationship, changes in $b will of course lead to changes in $source
This is a problem with PHP’s reference operator &. Because & is applied to variable $b when assigning value, $b does not copy “110” to itself but directly points to the nest of $source. From now on, $ The source is his $b. No matter how $source changes, it will lead to changes in $b - much like the relationship between a host and two monitors. Since this is the relationship, changes in $b will of course lead to changes in $source | Output result: 122, you know, these two variables are one "person" from now on, don't bully them!
In fact, this operator is more used in database connections, because when we create a database connection object, we often only need one, and too many are useless.