php = assignment operator _php techniques for different behavior of different data types

Source: Internet
Author: User
Tags print object

First explain the behavior of the assignment operator = See the following example:

Copy 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 you can see from this example, if the variable to 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; if the right variable is not a basic data type or an array, such as class, then a reference to the right variable is assigned to the left variable. Note: A reference to the right variable, not a reference to the content area referred to by the right variable; Look at the example below
Copy Code code as follows:

$a = new A ();
$b _a = $a;
$b _r = & $a;

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

The above example also shows that if you assign a value using the $var = & $a method, the $var=null to destroy the variable $var is in fact setting the contents of $var to NULL, In fact, it implies that any reference variable that points to the content area can be used to destroy the content of that content area. Therefore, to destroy the variable $var words with unset ($var). PS: In fact, a way to assign value $var is just a reference, can not take up much memory, or to destroy the so-called, here this is said to be destroyed by the unset way.

The following is an example of a "quote explanation" in the user's 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 pointing to $b or vice versa, but $a and $b pointing to the same place.
What is the reference?
Copy Code code as follows:

Referencing in PHP means accessing the contents of the same variable with a different name. This is not like a C pointer, but instead, the reference is a symbolic 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 file name and the file itself-the variable name is the directory entry, and the variable content is the file itself. A reference can be considered a tight connection in a Unix file system.

A little explanation of what "references are":

int i = 0;
int j = 0;
int *p = &i;
p = &j;
In the code above, p is a pointer to the memory address of I, and *p is the content; P=&j point changes the point of the P pointer, and the *P=111 expression changes the content of I. and PHP is not, the following example

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

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.