The difference between _php and reference is carefully explained by 5 PHP instances.

Source: Internet
Author: User
Keywords Pass a value pass a reference
Haha, will use only the initial stage, to understand the principle is what, so as to better use, crap not much said
Pass value: is to assign the value of the argument to the row parameter, then the modification of the row parameter will not affect the value of the argument
Pass-through reference: When a real address is passed through the parameter, both the row parameter and the argument are the same object, except that their name is different. Modification of the row parameter will affect the value of the argument
Description
Pass value: Root copy is the same. For example, I have a Yikanghuayuan house, I give you building materials, you built a root of my house the same house, you do nothing in your house will affect me, what I do in my house will not affect you, independent of each other.
Citation: It reminds me of the pointer to the C language when I was in college, and I felt pretty much the same. For example, I have a Yikanghuayuan house, I give you a key, two of us can enter this House, what you do in the House will affect me.
One, php instance
1, pass Value
Copy CodeThe code is as follows:
$param 1=1; Defining a Variable 1
$param 2=2; Defining a Variable 2
$param 2 = $param 1; Variable 1 assigned to variable 2
echo $param 2; Display as 1
?>

2, passing references
Copy CodeThe code is as follows:
$param 2=1; Defining a Variable 2
$param 1 = & $param 2; Pass a reference to the variable 2 to the variable 1
echo $param 2; Display as 1
$param 1 = 2; Assign a value of 2 to the variable 1
echo $param 2; Display as 2
?>

3, Function pass value
Copy CodeThe code is as follows:
Pass Value
$param 1 = 1; Defining a Variable 1
function Add ($param 2)//Pass Parameters
{
$param 2=3; Assign a value of 3 to the variable 2
}
$param 3=add ($param 1); Call the method add and pass the variable 1 to the variable 2
Echo '
$param 1== '. $param 1. '
'; Display as $param1==1
Echo '
$param 2== '. $param 2. '
'; Displayed as $param2== because $PARAM2 is a local variable, it cannot affect the global
Echo '
$param 3== '. $param 3. '
'; Display as $param3== because the Add method does not return a value, $param3 is empty
?>

4, Function Pass Reference
Copy CodeThe code is as follows:
Pass Value
$param 1 = 1; Defining a Variable 1
function Add (& $param 2)//Pass Parameters
{
$param 2=3; Assign a value of 3 to the variable 2
return $param 2; Return variable 2
}
Echo '
$param 1== '. $param 1. '
'; Show as $param1==1 no action on variable 1
$param 3=add ($param 1); Call the method add and pass the reference to the variable 1 to the variable 2
Echo '
$param 1== '. $param 1. '
'; The change in the $param 2 affects the variable 1, although no return is shown for the $PARAM1==3 call variable process
Echo '
$param 2== '. $param 2. '
'; Displayed as $param2== because of $param2 local variables, so it cannot affect the global
Echo '
$param 3== '. $param 3. '
'; Show as $param3== if you remove the return comment from the method, it will be $param3==3.
?>

5, Function Pass reference 2
Copy CodeThe code is as follows:
Pass Reference
$param 1 = 1;
Function &add (& $param 2)
{
$param 2 = 2;
return $param 2;
}
$param 3=&add ($param 1);
$param 4=add ($param 1);
Echo '
$param 3== '. $param 3. '
'; Display as $param3==2
Echo '
$param 4== '. $param 4. '
'; Display as $param4==2
Echo '
$param 1== '. $param 1. '
'; The change in $param 2 affects the variable 1 when it is displayed as a $param1==2 call variable
$param 3++;
/* shown below as $param1==3, this is because $param2 and $param1 refer to the same place,
* The return value is preceded by an address symbol or a reference $param3=&add ($param 1);
* So $param3, $param 2 and $param1 refer to the same place when $param3++;
* $param 1 will be changed */
Echo '
$param 1== '. $param 1. '
';
$param 4++;
/* shown below as $param1==3, why this is 3 instead of 4, because the return value is not preceded by
* Address symbol, which is not a reference so when $param4 changes, it does not affect $param1*/
Echo '
$param 1== '. $param 1. '
';
?>

Haha, but I think the citation will be better, less resource consumption. There is no obvious difference between the above test, probably because the test data is not large 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.