PHP references (&): A reference to a function returns

Source: Internet
Author: User

A reference to the function returns

Look at the code first:

<?phpfunction &test () {static $b =0;//declares a static variable $b= $b +1;echo $b; return $b;} $a =test ();//This statement will output $b value of 1$a=5, $a =test ();//This statement will output a value of $b 2$a=&test ();//This statement will output the value of $b 3$a=5; $a =test ();//This statement outputs $ The value of B is 6?>

The following explanation:

In this way $a=test (); The resulting is not a function of the reference return, which is not the same as the normal function call, as for the reason: this is the PHP provisions, PHP rules through $a=&test (); The way to get the function is to return the reference, as to what is a reference to return it (the PHP manual says: Reference return is used when you want to use a function to find the reference should be bound to the top of the variable.) This nonsense, I did not understand the day.

In the example above, the explanation is

$a =test () Call the function, just assign the value of the function to $ A, and $ A does nothing to affect the $b in the function, and the function is called by $a =&test () , and his function is to return $b The memory address of the $b variable and the memory address of the $ A variable point to the same place, which produces the equivalent effect "$a =& $b;" So changing the value of $ A also changes the value of $b, so the

$a =&test (); $a = 5;

Later, the value of the $b becomes 5.

This is to let you understand that the function reference returns only use static variables, in fact, the function of the reference return is more used in the object

Also attached is a PHP official example:

This is the the-the-pointer to access variable inside the Class.<?phpclass talker{    private $data = ' Hi '; 
   public function & Get () {        return $this->data;    }    Public function out () {        echo $this->data;    }   } $AA = new talker (); $d = & $aa->get (); $aa->out (); $d = ' how '; $aa->out (); $d = ' is '; $aa->out (); $d = ' you '; $aa ->out ();? >//the output is "hihowareyou"

PHP references (&): A reference to a function returns

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.