Detailed differences between passing values and transferring references using five php instances _ PHP Tutorial

Source: Internet
Author: User
Use five php instances to illustrate the differences between passing values and transferring references. Haha, it will only be used in the preliminary stage. we need to know what the principle is so that we can make better use of it. we will not talk much about passing values: it is to assign values of real parameters to row parameters, the modifications to the row parameter will be used only in the initial stage. we need to understand the principle 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, if I have a house, I will give you building materials, and you have built a house with the same root of my house, what you do in your house will not 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, if I have a house, and I give you a key, both of us can enter the house. what you do in the House will affect me.
I. php instance
1. pass the value

The 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

The 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

The 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

The 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

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

Values passed by limit: the value of the real parameter is assigned to the row parameter, so the modification of the row parameter is not...

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.