A small example that distinguishes PHP reference passing and value passing

Source: Internet
Author: User
Tags vars

Defined

    1. 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
    2. Pass-through reference: After the real address of the way parameters are passed, both the row and the arguments are the same object, except that their name is different, the modification of the row parameter will affect the value of the argument

Description

    1. Pass value: The same as copy. For example, I have a house, I give you building materials, you build a house that is the same as mine, you do nothing in your house will affect me, what I do in my house will not affect you, independent of each other.
    2. 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 house, I give you a key, two of us can enter the house, what you do in the House will affect me.

Instance

1, pass Value

    1. <?php
    2. $param 1=1; //define variables 1
    3. $param 2=2; //define Variables 2
    4. $param 2 = $param 1; //variable 1 assigned to variable 2
    5. echo $param 2; //Display as 1
    6. ?>



2, passing references

  1. <?php
  2. $param 2=1; //define Variables 2
  3. $param 1 = &$param 2; //Pass reference to variable 2 to variable 1
  4. echo $param 2; //Display as 1
  5. $param 1 = 2; //Assign 2 to the variable 1
  6. echo $param 2; //Display as 2
  7. ?>


3, Function pass value

  1. <?php
  2. Pass Value
  3. $param 1 = 1; //define variables 1
  4. function Add ($param 2) //Pass Parameters
  5. {
  6. $param 2=3; //Assign 3 to the variable 2
  7. }
  8. $param 3=add ($param 1); //Call method Add and pass the variable 1 to the variable 2
  9. echo ' <br> $param 1== '. $param 1.   ' <br> '; //Display as $param1==1
  10. echo ' <br> $param 2== '. $param 2.   ' <br> '; //Display as $param2== because $PARAM2 is a local variable, it cannot affect the global
  11. echo ' <br> $param 3== '. $param 3.   ' <br> '; //Display as $param3== because the Add method does not return a value, $param3 is empty
  12. ?>


4, Function Pass Reference

  1. <?php
  2. Pass Value
  3. $param 1 = 1; //define variables 1
  4. function Add (&$param 2) //Pass Parameters
  5. {
  6. $param 2=3; //Assign 3 to the variable 2
  7. return $param 2; Return variable 2
  8. }
  9. echo ' <br> $param 1== '. $param 1.   ' <br> '; //Display as $param1==1 no action on variable 1
  10. $param 3=add ($param 1); //Call method Add and pass a reference to variable 1 to the variable 2
  11. echo ' <br> $param 1== '. $param 1.   ' <br> '; //Show as $param1==3 call variable process, $param 2 change affects variable 1, although no return
  12. echo ' <br> $param 2== '. $param 2.   ' <br> '; //Display as $param2== because $PARAM2 local variables, it cannot affect the global
  13. echo ' <br> $param 3== '. $param 3.   ' <br> '; //Display as $param3== if the return comment inside the method is removed, it is $param3==3
  14. ?>


5, Function Pass reference 2

  1. <?php
  2. Pass Reference
  3. $param 1 = 1;
  4. Function &add (&$param 2)
  5. {
  6. $param 2 = 2;
  7. return $param 2;
  8. }
  9. $param 3=&add ($param 1);
  10. $param 4=add ($param 1);
  11. echo ' <br> $param 3== '. $param 3.   ' <br> '; //Display as $param3==2
  12. echo ' <br> $param 4== '. $param 4.   ' <br> '; //Display as $param4==2
  13. echo ' <br> $param 1== '. $param 1.   ' <br> '; //Show as $param1==2 call variable process, $param 2 Change effect variable 1
  14. $param 3++;
  15. /* shown below as $param1==3, this is because $param3 and $param1 refer to the same place,
  16. * The return value is preceded by an address symbol or a reference $param3=&add ($param 1);
  17. * So $param3, $param 2 and $param1 refer to the same place, when $param3++,
  18. * $param 1 will be changed */
  19. echo ' <br> $param 1== '. $param 1.      ' <br> ';
  20. $param 4++;
  21. /* shown below as $param1==3, why this is 3 instead of 4, because the return value is not preceded by
  22. * Address symbol, which is not a reference so when $param4 changes, it does not affect $param1*/
  23. echo ' <br> $param 1== '. $param 1.    ' <br> ';
  24. ?>

A small example that distinguishes PHP reference passing and value passing

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.