Php reference value transfer learning Notes

Source: Internet
Author: User
In php, you only need to add & amp; before the original object to assign values by reference, $ a & amp; $ B; in fact, the reference in php is that two variables with different names point to the same value. Reference what is in PH

In php, you only need to add one & before the original object to assign values by reference, $ a = & $ B; in fact, the reference in php is that two variables with different names point to the same value.

Reference

Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. A reference can be considered as a hardlink in a Unix file system.

I. variable reference,The code is as follows:

  1. $ A = 100;
  2. $ B = & $;
  3. Echo $ B; // output 100
  4. Echo $ a; // output 100 here, indicating that the values of $ a and $ B are both one hundred.
  5. $ B = 200;
  6. Echo $ a; // output 200
  7. Echo $ B; // output 200 here, which means they use the same address. Change one, and the other will also change.
  8. ?>

2. reference and pass the value in the function,The code is as follows:

  1. Function main ($ a, $ B ){
  2. $ B = $ a + 100;
  3. Return $ B;
  4. }
  5. Main (55, & $ B); // Here $ B is actually passing its memory address to the $ B parameter in the main function, the value of $ B is changed by changing the parameter $ B.
  6. Echo $ B; // The output is 155,
  7. ?>

3. pass the reference value of the object,The code is as follows:

  1. Class club {
  2. Var $ name = "real madrid ";
  3. }
  4. $ B = new club;
  5. $ C = $ B;
  6. Echo $ B-> name; // output real madrid
  7. Echo $ c-> name; // output real madrid
  8. $ B-> name = "ronaldo ";
  9. Echo $ c-> name; // output ronaldo
  10. ?>

Cancel reference:When you unset a reference, you just disconnect the binding between the variable name and the variable content, which does not mean that the variable content is destroyed, for example, the following code:

  1. $ A = 'Ronaldo'
  2. $ B = & $;
  3. Unset ($ );
  4. ?>

It won't unset $ B, just $ a, for example, reference transfer, test1.php, the code is as follows:

  1. /**
  2. * Reference transfer
  3. The following content can be passed through reference:
  4. Variable, such as foo ($)
  5. New statement, such as foo (new foobar ())
  6. Reference returned from the function, for example:
  7. */
  8. Function foo (& $ var)
  9. {
  10. $ Var ++;
  11. }
  12. $ A = 5;
  13. // Valid
  14. Foo ($ );
  15. Foo (new stdClass ());
  16. // Illegal use
  17. Function bar () // Note the missing &
  18. {
  19. $ A = 5;
  20. Return $;
  21. }
  22. Foo (bar (); // fatal error caused by PHP 5.0.5
  23. Foo ($ a = 5) // expression, not a variable
  24. Foo (5) // cause a fatal error
  25. ?>

The code for test2.php is as follows:

  1. Function test (& $)
  2. {
  3. $ A = $ a + 100;
  4. }
  5. $ B = 1;
  6. Echo $ B; // output 1
  7. Test ($ B); // here, $ B actually transmits the memory address of the variable content of $ B to the function, you can change the value of $ B by changing the value of $ a in the function.
  8. Echo"
    ";
  9. Echo $ B; // output 101
  10. /*****************************
  11. *
  12. * Note that the parameter after call_user_func_array is required &
  13. *
  14. *****************************/
  15. // Do not add the & symbol before $ B in the above "test ($ B);", but in the function "call_user_func_array", to reference the parameter passing, the & symbol is required, as shown in the following code:
  16. Function a (& $ B ){
  17. $ B ++;
  18. }
  19. $ C = 0;
  20. Call_user_func_array ('A', array (& $ c ));
  21. Echo $ c;
  22. // Output 1
  23. ?>

Reference return

When you want to use a function to find the variable to which the reference should be bound, do not use the return reference to increase performance. the engine is smart enough to optimize it, A reference is returned only when there are reasonable technical reasons! To return a reference, use the following syntax:

  1. Function & test ()
  2. {
  3. Static $ B = 0; // declare a static variable
  4. $ B = $ B + 1;
  5. Echo $ B;
  6. Return $ B;
  7. }
  8. $ A = test (); // This statement outputs the value of $ B as 1.
  9. $ A = 5;
  10. $ A = test (); // This statement outputs the value of $ B to 2.
  11. $ A = & test (); // This statement outputs the value of $ B as 3. here, the memory address of the $ B variable in return $ B is pointed to the same place as the memory address of the $ a variable.
  12. $ A = 5; // The value of the $ B variable in return $ B has been changed.
  13. $ A = test (); // This statement outputs a value of 6 for $ B.
  14. /**
  15. ?>

The following explains:

In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the PHP rule.

PHP requires that $ a = & test (); is used to obtain the function reference and return.

As for what is reference return (in the PHP Manual, reference return is used when you want to use a function to find the variable on which the reference should be bound, I haven't understood it for half a day

The example above is as follows: $ a = test () is used to call a function. it only assigns the value of the function to $, $ a does not affect $ B in the function, but calls the function in the $ a = & test () method, the function is to direct the memory address of the $ B variable in return $ B to the memory address of the $ a variable, that is, the equivalent effect ($ a = & $ B;) is generated. Therefore, the value of $ a is changed, and the value of $ B is also changed.

$ A = & test (); $ a = 5; later, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.

An interesting example is shown on oschina. the code is as follows:

  1. $ A = array ('Abe ', 'Ben', 'Cam ');
  2. Foreach ($ a as $ k ==>&$ n)
  3. $ N = strtoupper ($ n );
  4. Foreach ($ a as $ k =>$ n) // notice NO reference here!
  5. Echo "$ nn ";
  6. Print_r ($ );
  7. ?>
  8. Will result in:
  9.  
  10. ABE
  11. BEN
  12. BEN
  13. Array
  14. (
  15. [0] => ABE
  16. [1] => BEN
  17. [2] => BEN
  18. )
  19. // Explanation: the loop in the second foreach is as follows:
  20. Array
  21. (
  22. [0] => ABE
  23. [1] => BEN
  24. [2] => ABE
  25. )
  26. Array
  27. (
  28. [0] => ABE
  29. [1] => BEN
  30. [2] => BEN
  31. )
  32. Array
  33. (
  34. [0] => ABE
  35. [1] => BEN
  36. [2] => BEN
  37. )

Because there is no unset ($ n), it always points to the last element of the array. the first loop in the second foreach changes $ n, that is, $ a [2] to ABE, the second cycle is changed to BEN, and the third cycle is BEN.

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.