Functions of variables and Functions & symbols

Source: Internet
Author: User

First, let's take a look at the following example:

<?php$a="val1";$b="val2";$a=&$b;echo $a."<br/>";$b="123";echo $a;?>

In the preceding example, If you replace $ A = & $ B with $ A = $ B, the two outputs of $ A have the same value, but with the & Symbol added, $ the memory addresses of B and $ a point to the same memory, therefore, if you change $ B $ A, it will also change! If the value of $ A changes, the output of $ B also changes accordingly!

Let's look at the example below.

<? Phpfunction & Test () {static $ B = 0; // declare a static variable $ B = $ B + 1; echo $ B; return $ B ;} $ A = test (); // This statement will output the value of $ B as 1 $ A = 5; $ A = test (); // This statement outputs the value of $ B as 2 $ A = & Test (); // This statement outputs the value of $ B as 3 $ A = 5; $ A = test (); // This statement outputs the value of $ B as 6?>

 

In this way, $ A = test (); does not actually return a function reference, which is no different from a common function call.

As for the reason: this is a 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 .)

The example above is as follows:
$ A = test () is used to call a function. It only assigns the value of the function to $ A. Any change made to $ A does not affect $ B in the function.
In the $ A = & Test () method, the function calls the memory address of the $ B Variable in return $ B and the memory address of the $ a variable,
Point to the same place. this is equivalent to the effect ($ A = & B;). Therefore, the value of $ A is changed and the value of $ B is changed. Therefore, the following code is executed:
$ A = & Test (); $ A = 5; then, the value of $ B is changed to 5.

Functions of variables and Functions & symbols

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.