Data type = assignment operator in PHP has different behaviors for different data types

WBOY
Release: 2016-07-29 08:44:11
Original
1187 people have browsed it

First explain the behavior of assignment operator =, look at the following example:

Copy code The code is as follows:


$i = 0;
$j = $i;
$j = 0;
echo $ j; //Print output 0
$arr = array(0);
$arr2 = $arr;
$arr2[0] = 1;
echo $arr[0]; //Print output 0
class B
{
public $i = 0;
}
$b = new B();
$c = $b;
$c->i = 1;
echo($b->i); // printout 1


It can be seen from this example that if the variable on the right side of the = operator is a basic data type or array, then the = operator assigns a copy of the right variable to the left variable; if the right variable is not a basic data type or array , such as class, then = will assign a reference to the variable on the right to the variable on the left. Note: It is a reference to the variable on the right, not to the content area pointed to by the variable on the right; see the example below for details

Copy the code The code is as follows:


$a = new A();
$b_a = $a;
$b_r = &$a;
$b_a = null;
var_dump($a); //Print object(A)[2], the content pointed to by $a is still there
$b_r = null;
var_dump($a); // Print null, the content pointed to by $a is cleared


The above example also shows that if you use $var = &$a to assign a value, use $var= If null is used to destroy the variable $var, it actually sets the content pointed to by $var to null. In fact, this sentence also implies that any reference variable pointing to the content area can be used to destroy the content of the content area. Therefore, to destroy the variable $var, use unset($var). PS: In fact, assigning $var in this way is just a reference and does not take up much memory. It does not matter whether it is destroyed or not. This means that it must be destroyed by unset.
The following is an example of "Quotation Explanation" in the "User Manual":
$a =& $b;
There is an explanation below:
This means that $a and $b point to the same variable.
Note: $a and $b are exactly the same here. It’s not that $a points to $b or vice versa, but that $a and $b point to the same place.
What is a quote?

Copy the code The code is 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 tight connections in a Unix file system.


A little explanation about "what is a reference":
int i = 0;
int j = 0;
int *p = &i;
p = &j;
In the above code, p is a memory pointing to i The pointer of the address, and *p is the content; p=&j points to change the pointer of p, and the expression of *p=111 will change the content of i. This is not the case in PHP. The following example
$i = 0;
$p = &$i;
$p = 111 will immediately change the value of $i.

The above introduces the different behaviors of the data type = assignment operator in PHP for different data types, including the content of data types. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!