Differences between passing a value in php and transferring a reference

Source: Internet
Author: User
There is a big difference between passing a value in php and passing a reference. a reference can call the memory address to assign a value, in this way, as long as the content in the memory address changes and the variable assigned by the value changes, the value is only used to assign the value of the memory to other variables.

There is a big difference between passing a value in php and passing a reference. a reference can call the memory address to assign a value, in this way, as long as the content in the memory address changes and the variable assigned by the value changes, the value is only used to give the memory value to other variables.

Pass value:

The parameter stack is a copy of the parameter.

Any modification works on the copy. it does not work on the original variable.

Reference:

The stack is a referenced copy. because the reference points to a variable, the reference operation is actually an operation on the variable it points, just reference the grass paper with less understanding of pointers)

The sample code is as follows:

  1. Function func1 ($ a) {$ a = $ a + 1 ;}
  2. Function func2 (& $ a) {$ a = $ a + 1 ;}
  3. $ Sample = 1;
  4. Func1 ($ sample );
  5. Echo $ sample; // output 1
  6. $ Sample = 1;
  7. Func2 ($ sample );
  8. Echo $ sample; // output 2
  9. // The sample code is as follows:
  10. $ Num1 = 15;
  11. $ Num2 = & $ num1;
  12. $ Num2 = 20;
  13. Echo $ num1; // output 20
  14. ?>
  15. // The following code is used:
  16. Function func1 ($ ){
  17. $ A = $ a + 1;
  18. }
  19. Function func2 (& $ ){
  20. $ A = $ a + 1;
  21. }
  22. $ Sample = 1;
  23. Func1 ($ sample );
  24. Echo $ sample; // output 1
  25.  
  26. $ Sample = 1;
  27. Func2 ($ sample );
  28. Echo $ sample; // output 2
  29. ?>

Summary:If a value is transferred, if it is not an object, a copy of the value will be carried out. any changes to this variable will not affect the original value. The reference or object will be transferred, which is the real memory address of the fax, the changes made to this variable will affect the original value.

Related Article

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.