Home > Backend Development > PHP Tutorial > Different behaviors of = assignment operator for different data types in PHP_PHP Tutorial

Different behaviors of = assignment operator for different data types in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:32:52
Original
898 people have browsed it

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

Copy the 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); // Print output 1

As can be seen from this example, if the variable on the right side of the = operator is a basic data type or array, then the = operator takes a copy of the variable on the right Copy assignment 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 right variable to the left variable. 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 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
$b_r = null;
var_dump($a); //Print null, the content pointed by $a is still there The pointed content is cleared

The above example also shows that if you use $var = &$a to assign a value, using $var=null to destroy the variable $var will actually destroy it. The content pointed to by $var is set 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 "quoted explanation" in the "User Manual":

$a =& $b;
There is such an explanation below:
This means So $a and $b point to the same variable.
Note: $a and $b are exactly the same here. This does not mean 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 pointer pointing to the memory address of i, and *p is its content; p=&j points to change the pointing of the p pointer, expressed as *p=111 The formula will change the content of i. This is not the case in PHP. In the following example,

$i = 0;
$p = &$i;
$p = 111 will immediately change the value of $i.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322714.htmlTechArticleFirst explain the behavior of the assignment operator =, look at the following example: Copy the code The code is as follows: $i = 0; $j = $i; $j = 0; echo $j; // Print output 0 $arr = array(0); $arr2 = $arr; $arr2[0...
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