Data type = assignment operator different behavior for different data types in PHP

Source: Internet
Author: User
Tags print object
First explain the behavior of the assignment operator =, see the following example:

Copy the Code code as follows:


$i = 0;
$j = $i;
$j = 0;
Echo $j; Print output 0
$arr = Array (0);
$arr 2 = $arr;
$arr 2[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 of the = operator is a basic data type or an array, then the = operator assigns a copy of the right variable to the left variable, and if the right variable is not a base data type or array, such as class, then = assigns a reference to the right variable to the left variable. Note: It is a reference to the right variable, not to the content area referred to by the right variable; see below for an example

Copy the Code code as follows:


$a = new A ();
$b _a = $a;
$b _r = & $a;
$b _a = null;
Var_dump ($a); Print object (A) [2], $a pointing to the contents of the
$b _r = null;
Var_dump ($a); Print null, $a the content pointed to is cleared


The above example also shows that if you use the $var = & $a to assign a value, using $var=null to destroy the variable $var is actually to $var the content is set to NULL, In fact, it implies that any reference variable pointing to the content area can be used to destroy the contents of the content area. So, to destroy the variable $var words with unset ($var). PS: In fact, this way to assign value $var is just a reference, not a lot of memory, or to destroy not so-called, here This is said to be destroyed in unset way.
The following is an example of "cited explanations" in the User guide:
$a =& $b;
There is a sentence below to explain:
This means that $a and $b point to the same variable.
Note: $a and $b are exactly the same here, not $a point to $b or vice versa, but $a and $b point to the same place.
What is a reference?

Copy the Code code as follows:


Referencing in PHP means accessing the same variable content with a different name. This is not like the C pointer, instead, the reference is the symbol table alias. Note that in PHP, variable names and variable contents are not the same, so the same content can have different names. The closest analogy is the Unix filename and the file itself-the variable name is the directory entry, and the variable content is the file itself. References can be seen as tight connections in Unix file systems.


A little explanation of what "references are":
int i = 0;
int j = 0;
int *p = &i;
p = &j;
In the above code, p is a pointer to the memory address of I, and *p is the content, and the p=&j point changes the direction of the P pointer, and the *P=111 expression changes the contents of I. While PHP is not, the following example
$i = 0;
$p = & $i;
$p = 111 will change the value of the $i immediately.

The above describes the data type in PHP = assignment operator for different data types of different behavior, including the data type aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.