"Go" php function quote

Source: Internet
Author: User
"Go" php function reference

Reprinted from Hosserer

Final edit? hosserer

?

An image of metaphor:

Reference: You are my shadow, I wear what clothes you are also what clothes, you are unset, I am still me.

Value: You are a photo of my time, or you are a separate thing, I say you are an apple, you are an apple, and I have no relationship.

The following is reproduced:

?

The PHP function in front of the & symbol means that the function of the reference back, the PHP function before the addition of the & symbol to what effect?

PHP code?

function &test ()
{
Static $b =0;//declaration of a statically variable
$b = $b +1;
Echo $b;
return $b;
}

$a =test ();//This statement outputs a value of $b of 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 will output a value of $b of 6?
The following explanation:
In this way $a=test () is not actually returned by a function reference, which is not the same as a normal function call.

As for the reason: this is the PHP rule
PHP rules through $a=&test (); The way to get is to return the reference to the function.

As for 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.) )

In the example above, the explanation is
$a =test () call the function, just assign the value of the function to $ A, and no change to $ A will affect the $b in the function.
And by calling the function by $a=&test (), his function is to return the memory address of the $b variable in the $b and the memory address of the $ A variable,
Point to the same place. That produces the equivalent effect ($a =&b;) So change the value of $ A and change the value of $b at the same time, so in execution:
$a =&test (); $a = 5; Later, the value of the $b becomes 5 ...


What does the & symbol in front of the PHP variable mean?

Look at an example first

PHP code?
$foo = 321;???
$bar = & $foo;????
$bar = 123;???
Print $foo;

$foo = 321;
$bar = & $foo;?
$bar = 123;
print $foo; So what's the result of the output?

PHP code?
123????

Why is that?

Changing the new variable will affect the original variable, and this assignment is faster
Note: Only named variables can pass address assignment
That is, changing the value of the $bar will change the value of the $foo.

  • 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.