PHP = different actions of the value assignment operator on different data types

Source: Internet
Author: User
The assignment operator in PHP performs different actions on different data types. First, explain the behavior of the value assignment operator =. See the following example: Copy the code as follows: $ i0; $ j $ I; $ j0; echo $ j; print the output 0 $ arrarray (0); $ arr2 $ arr; $ arr2 [0 First explains the behavior of the value assignment operator =. See the following example:

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

The 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?

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

The export code is as follows: $ I = 0; $ j = $ I; $ j = 0; echo $ j; // Print the output 0 $ arr = array (0 ); $ arr2 = $ arr; $ arr2 [0...

Related Article

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.