In PHP, The = value assignment operator performs different actions on different data types.

Source: Internet
Author: User

First, explain the behavior of the value assignment operator =. Let's look at the example below:
Copy codeThe 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 the output 0

Class B
Public $ I = 0;

$ B = new B ();
$ C = $ B;
$ C-> I = 1;
Echo ($ B-> I); // Print Output 1

From this example, we can see that if the variable on the right of the = Operator is of the basic data type or array, the = Operator will assign a copy of the variable on the right to the variable on the left; if the variable on the right is not a basic data type or array, such as class, = will assign a reference to the variable on the right to the variable on the left. Note: it refers to the reference of the right variable, not the reference of the content area pointed to by the right variable. For details, refer to the example below.
Copy codeThe Code is as follows:
$ A = new ();
$ B _a = $;
$ B _r = & $;

$ B _a = null;
Var_dump ($ a); // print the object (A) [2]. The content pointed to by $ a is still in
$ 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 values, if $ var = null is used to destroy the variable $ var, the content indicated by $ var is actually set to null, in fact, this statement also implies that any referenced 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, the assignment of $ var in this way is just a reference, and it does not occupy much memory. Whether to destroy it or not is not called. In this case, it must be destroyed by unset.

The following is an example of "referenced interpretation" in the user manual:

$ A = & $ B;
Below is an explanation:
This means that $ a and $ B point to the same variable.
Note: $ a and $ B are exactly the same here. This doesn't mean $ a points to $ B or vice versa, but $ a and $ B point to the same place.
What is a reference?
Copy codeThe Code is as follows:
Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. A reference can be considered as a tight connection in a Unix file system.

What is the reference:

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 of it; p = & j points to change the pointer of p, use the * p = 111 expression to change the content of I. PHP does not. The example below

$ I = 0;
$ P = & $ I;
$ P = 111, the value of $ I will be changed immediately.

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: 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.