Value Transfer and reference transfer in PHP

Source: Internet
Author: User

 

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:

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, Reference Transmission

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.

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

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.