Value Transfer and reference transfer in PHP. Value Transfer and reference transfer exist in PHP. The latter must use an address character to identify the variable. An example of the value assignment operation is as follows: 1. value transfer 1) basic data type: 01? Php02 $ a PHP contains value transfer and reference transfer. 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
02
$ A = 1;
03
$ B = $;
04
$ B + = 2;
05
06
Echo "\ $ a =". $ ."
";
07
Echo "\ $ B =". $ B ."
";
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 ().".
";
2
Echo "\ $ p3's". $ p3-> toString ().".
";
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
02
$ A = 1;
03
$ B = & $;
04
$ B + = 2;
05
06
Echo "\ $ a =". $ ."
";
07
Echo "\ $ B =". $ B ."
";
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 ().".
";
2
Echo "\ $ p3's". $ p3-> toString ().".
";
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
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 ().".
";
27
Echo "\ $ p3's". $ p3-> toString ().".
";
28
?>
2) basic data types
1
2
$ A = 1;
3
$ B = $;
4
// $ B = & $;
5
$ B + = 2;
6
7
Echo "\ $ a =". $ ."
";
8
Echo "\ $ B =". $ B ."
";
9
?>
Conclusion: the transfer of values and references are similar to the assignment operation when the methods and function parameters are passed.
Bytes. The latter must use an address character to identify the variable. An example of the value assignment operation is as follows: 1. value transfer 1) basic data type: 01? Php 02 $...