How reference notation (&) in PHP is used in the code explanation

Source: Internet
Author: User
PHP refers to a variable or function, object, and so on before the & symbol, in PHP refers to the meaning: different names to access the same variable content.

There is a difference between the pointers in the C language. The pointer inside the C language stores a reference to the address variable where the contents of the variable are stored in memory.
PHP references allow you to use two variables to point to the same content

$a = "ABC"; $b =& $a; echo $a;//output here: ABC echo $b;//output here: ABC $b = "EFG"; echo $a;//Here the value of $ A becomes EFG so the output EFG echo $b;//Output EFG here

Invocation of a function's address
Address call I'm not going to say much. Directly below the code

Function test (& $a) {$a = $a +100;} $b = 1; echo $b;//Output 1 Test ($b);   Here $b passed to the function is actually the memory address of the $b variable content, by changing the value of $ A in the function can change the value of the $b echo "<br>"; echo $b;//Output 101

It is important to note that here Test (1), the words will be wrong, reason to think
A reference to the function returns
Look at the code first

function &test () {static $b =0;//declares a static variable $b = $b +1; echo $b; return $b;} $a =test ();//This statement outputs $b value 1 $a = 5; $a =test ();//This statement outputs a value of $b of 2 $a =&test ();//This statement outputs a value of $b of 3 $a = 5; $a =test ();//This statement outputs a value of $b of 6

Explained below:
in this way $a=test (); The resulting is not a function of the reference to return, which is not the same as the normal function call the reason: this is the PHP provisions
PHP provisions through $a=&test ();
As to what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) I didn't read this shit for a long while.
using the example above to explain is
$a =test () call the function, just assign the value of the function to $ A, and $ A does not affect $b
in the function and call the function by $a=&test (). , his role is to refer to the memory address of the $b variable in the return $b to the same place as the memory address of the $ A variable
that produces the equivalent effect ($a =&b;) So changing the value of $ A also changes the value of $b so that it executes the
$a =&test ();
$a = 5; After the
, the value of the $b becomes 5.

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.