function decomposition of pre-& symbol in PHP

Source: Internet
Author: User

This article mainly introduces the function of PHP in the pre-add & symbol function decomposition, its function is referred to return, a bit abstract, detailed explanation please see the contents of this article, the need for Friends can refer to (reprint)

PHP variables in front of add & symbol is not to say, we all in use, is two variables at the same time point to an address, then, the PHP function in front of the meaning of the & symbol is what? Let's start with two demo code and then explain.

function&Chhua () {Static $b= "Www.jb51.net";//declare a static variable    $b=$b. "Web Development"; Echo $b; return $b;}$a= Chhua ();//This statement will output the value of the $b "Www.jb51.netWEB development"$a= "PHP";Echo"<Br>";$a= Chhua ();//This statement outputs $b value of "Www.jb51.netWEB development web Development"Echo"<Br>";$a= &chhua ();//This statement outputs the value of the $b "Www.jb51.netWEB developing Web development Web Development"Echo"<Br>";$a= "JS";$a= Chhua ();//This statement will output the value of the $b "Jsweb development"function&Test () {Static $b= 0;//declare a static variable    $b=$b+ 1; Echo $b; return $b;}$a= Test ();//This statement will output a value of 1 for the $b$a= 5;$a= Test ();//This statement will output a value of 2 for the $b$a= &test ();//This statement will output a value of 3 for the $b$a= 5;$a= Test ();//This statement will output a value of 6 for the $b

Let's explain the second function.
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 $b becomes 5.

function decomposition of pre-& symbol in PHP

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.