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:
01
<? Php
02
$ A = 1;
03
$ B = $;
04
$ B + = 2;
05
06
Echo "\ $ a =". $ a. "<br/> ";
07
Echo "\ $ B =". $ B. "<br/> ";
08
09
/* Output:
10
$ A = 1
11
$ B = 3
12
*/
13
?>
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:
01
Class Person {
02
Private $ _ name;
03
04
Public function setName ($ name ){
05
$ This-> _ name = $ name;
06
}
07
08
Public function getName (){
09
Return $ this-> _ name;
10
}
11
12
Public function toString (){
13
Return "name is". $ this-> _ name;
14
}
15
}
Initialize two Person class objects and set attributes:
1
$ P1 = new Person ();
2
$ P1-> setName ("person1 ");
3
$ P2 = new Person ();
4
$ P2-> setName ("person2 ");
Perform the following operations:
1
$ P3 = $ p1;
Test:
1
Echo "\ $ p1's". $ p1-> toString (). ". <br/> ";
2
Echo "\ $ p3's". $ p3-> toString (). ". <br/> ";
3
4
/* Output:
5
$ P1's name is person1.
6
$ P3's name is person1.
7
*/
NOTE: For the $ p3 = $ p1 statement, the value assigned is 0x000a. That is, $ p1 and $ p3 both reference the same object.
2. Transfer references
1) Basic Data Types
01
<? Php
02
$ A = 1;
03
$ B = & $;
04
$ B + = 2;
05
06
Echo "\ $ a =". $ a. "<br/> ";
07
Echo "\ $ B =". $ B. "<br/> ";
08
09
/* Output:
10
$ A = 3
11
$ B = 3
12
*/
13
?>
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
1
$ P3 = & $ p1;
Then, perform the following operations:
1
$ P3 = $ p2;
The test results are as follows:
1
Echo "\ $ p1's". $ p1-> toString (). ". <br/> ";
2
Echo "\ $ p3's". $ p3-> toString (). ". <br/> ";
3
4
/* Output:
5
$ P1's name is person2.
6
$ P3's name is person2.
7
*/
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.
The complete code is as follows:
1) Reference Data Type
01
<? Php
02
Class Person {
03
Private $ _ name;
04
05
Public function setName ($ name ){
06
$ This-> _ name = $ name;
07
}
08
09
Public function getName (){
10
Return $ this-> _ name;
11
}
12
13
Public function toString (){
14
Return "name is". $ this-> _ name;
15
}
16
}
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
1
<? Php
2
$ A = 1;
3
$ B = $;
4
// $ B = & $;
5
$ B + = 2;
6
7
Echo "\ $ a =". $ a. "<br/> ";
8
Echo "\ $ B =". $ B. "<br/> ";
9
?>
Conclusion: The transfer of values and references are similar to the assignment operation when the methods and function parameters are passed.