Value Transfer and reference transfer exist in PHP. The latter must use the address operator & to identify the variable. The following is an example of assigning values:
1, Value transfer
1) Basic data types:
<? PHP $ A = 1; $ B = $ A; $ B + = 2; echo"\ $ A =". $ ."<Br/>"; Echo"\ $ B =". $ B ."<Br/>";/* Output: $ A = 1 $ B = 3 */?>
Note: The $ B = $ a statement is used to assign the value of $ A to $ B. That is, the value assigned is 1.
After the statement $ B + = 2 is executed:
Therefore, $ B is changed to 3, but the value of $ A is not changed.
2) Reference Data Type
The person class is as follows:
<?PHP class person {private $ _ name; Public Function setname ($ name) {$ this-> _ name = $ name;} public function getname () {return $ this-> _ name;} public function tostring () {return"Name is". $ This-> _ name ;}}?>
Initialize two person class objects and set attributes:
3 |
$ P2 = new person (); |
4 |
$ P2-> setname ("person2 "); |
Perform the following operations:
Test:
1 |
Echo "\ $ P1's". $ P1-> tostring (). ". <br/> "; |
2 |
Echo "\ $ P3's". $ P3-> tostring (). ". <br/> "; |
5 |
$ P1's name is person1. |
6 |
$ P3's name is person1. |
NOTE: For the $ P3 = $ P1 statement, the value assigned is 0x000a. That is, $ P1 and $ P3 both reference the same object.
2, Reference Transmission
1) Basic Data Types
03 |
$ B = & $; |
04 |
$ B + = 2; |
05 |
|
06 |
Echo "\ $ A =". $ A. "<br/> "; |
07 |
Echo "\ $ B =". $ B. "<br/> "; |
08 |
|
Note: The $ B = & $ a statement is used for reference transfer. That is, the value assigned is 0x0001.
So after $ B + = 2,
Therefore, the values of $ A and $ B are both 3.
2) Reference Data Type
If you perform the following operations to replace the previous $ P3 = $ p1
Then, perform the following operations:
The test results are as follows:
1 |
Echo "\ $ P1's". $ P1-> tostring (). ". <br/> "; |
2 |
Echo "\ $ P3's". $ P3-> tostring (). ". <br/> "; |
5 |
$ P1's name is person2. |
6 |
$ P3's name is person2. |
Note: the result of $ P3 is clear, because $ P3 = $ P2 is executed, but why is the result of printing $ P1 different from the previous one?
It is because after the $ P3 = & $ P1 statement is executed, the value assigned is 0x0001.
Then run the $ P3 = $ P2 statement.
Therefore, the above result information is printed. This is the address operator.
CompleteCodeAs follows:
1) Reference Data Type
01 |
<? PHP |
02 |
Class person { |
05 |
Public Function setname ($ name ){ |
06 |
$ This-> _ name = $ name; |
09 |
Public Function getname (){ |
10 |
Return $ this-> _ name; |
13 |
Public Function tostring (){ |
14 |
Return "name is". $ this-> _ name; |
17 |
|
18 |
$ P1 = new person (); |
19 |
$ P1-> setname ("person1 "); |
20 |
$ P2 = new person (); |
21 |
$ P2-> setname ("person2 "); |
22 |
|
23 |
$ P3 = $ P1; |
24 |
// $ P3 = & $ P1; |
25 |
// $ P3 = $ P2; |
26 |
Echo "\ $ P1's". $ P1-> tostring (). ". <br/> "; |
27 |
Echo "\ $ P3's". $ P3-> tostring (). ". <br/> "; |
28 |
?> |
2) Basic Data Types
3 |
$ B = $; |
4 |
// $ B = & $; |
7 |
Echo "\ $ A =". $ A. "<br/> "; |
8 |
Echo "\ $ B =". $ B. "<br/> "; |
Conclusion: The transfer of values and references are similar to the assignment operation when the methods and function parameters are passed.