Detailed descriptions of the differences between passing a value and transferring a reference using five php instances

Source: Internet
Author: User
Today, a colleague asked me what is the difference between passing a value and passing a reference. This reminds me that when I first learned php, I had done a lot of projects and made many things, I thought I had almost mastered php. Over time, the deeper I learned, the more I felt that I knew very little about it. haha, I would use it as a preliminary stage, we need to know what the principle is so that we can make better use of it.
Pass value: if the value of the real parameter is assigned to the row parameter, the modification of the row parameter will not affect the value of the real parameter.
Pass reference: After passing parameters in real address mode, the row parameters and real parameters are the same object, but their names are different. modifying the row parameters will affect the values of the real parameters.
Note:
Pass value: the root copy is the same. For example, do I have one? H House, I will give you building materials. you have built a house with the same root of my house. nothing you do in your house will affect me, what I do in my house will not affect you and be independent of each other.
Reference: Reminds me of the pointer to C language when I went to college. it feels similar. For example, do I have one? H House. I'll give you a key. both of us can enter this House. what you do in the House will affect me.
I. php instance
1. pass the value
Copy codeThe code is as follows:
$ Param1 = 1; // Define Variable 1
$ Param2 = 2; // defines variable 2
$ Param2 = $ param1; // variable 1 is assigned to variable 2.
Echo $ param2; // 1
?>

2. Upload reference
Copy codeThe code is as follows:
$ Param2 = 1; // defines variable 2
$ Param1 = & $ param2; // pass the reference of variable 2 to variable 1
Echo $ param2; // 1
$ Param1 = 2; // assign 2 to variable 1
Echo $ param2; // 2
?>

3. pass the function value
Copy codeThe code is as follows:
// Pass the value
$ Param1 = 1; // Define Variable 1
Function add ($ param2) // pass parameters
{
$ Param2 = 3; // assign 3 to variable 2
}
$ Param3 = add ($ param1); // call the add method and pass variable 1 to variable 2.
Echo'
$ Param1 = '. $ param1 .'
'; // Display as $ param1 = 1
Echo'
$ Param2 = '. $ param2 .'
'; // Display as $ param2 = because $ param2 is a local variable, it cannot affect the global
Echo'
$ Param3 = '. $ param3 .'
'; // Display as $ param3 = because the add method does not return a value, $ param3 is empty
?>

4. function transfer reference
Copy codeThe code is as follows:
// Pass the value
$ Param1 = 1; // Define Variable 1
Function add (& $ param2) // pass parameters
{
$ Param2 = 3; // assign 3 to variable 2
// Return $ param2; // return variable 2
}
Echo'
$ Param1 = '. $ param1 .'
'; // The result is displayed as $ param1 = 1. no operation is performed on variable 1.
$ Param3 = add ($ param1); // call the add method and pass the reference of variable 1 to variable 2.
Echo'
$ Param1 = '. $ param1 .'
'; // Display as $ param1 = 3 during the call of the variable, the change of $ param2 affects variable 1, although no return
Echo'
$ Param2 = '. $ param2 .'
'; // Display as $ param2 = because $ param2 is a local variable, it cannot affect the global
Echo'
$ Param3 = '. $ param3 .'
'; // Display as $ param3 = if you remove the return comment in the method, it will be $ param3 = 3
?>

5. function transfer reference 2
Copy codeThe code is as follows:
// Upload reference
$ Param1 = 1;
Function & add (& $ param2)
{
$ Param2 = 2;
Return $ param2;
}
$ Param3 = & add ($ param1 );
$ Param4 = add ($ param1 );
Echo'
$ Param3 = '. $ param3 .'
'; // Display as $ param3 = 2
Echo'
$ Param4 = '. $ param4 .'
'; // Display as $ param4 = 2
Echo'
$ Param1 = '. $ param1 .'
'; // Display as $ param1 = 2 during the call of the variable, $ param2 changes affect variable 1
$ Param3 ++;
/* $ Param1 = 3 is shown below, because $ param2 and $ param1 reference to the same place,
* The return value is preceded by an address symbol or a reference $ param3 = & add ($ param1 );
* In this way, $ param3, $ param2, and $ param1 are referenced in the same place. when $ param3 ++,
* $ Param1 will be changed */
Echo'
$ Param1 = '. $ param1 .'
';
$ Param4 ++;
/* The following shows $ param1 = 3. Why is it 3 instead of 4? this is because there is no
* The address symbol, which is not a reference. Therefore, $ param1 */is not affected when $ param4 is changed */
Echo'
$ Param1 = '. $ param1 .'
';
?>

Haha, but I think it would be better to pass the reference and consume less resources. There is no obvious gap in the above test, probably because the test data is not big enough. if there is more data to test, I think there will be a significant difference.

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.