In php, the function is decomposed by adding & symbol before it. php function _ PHP Tutorial

Source: Internet
Author: User
In php, the function is decomposed by adding the & amp; symbol before it. it is a php function. In php, the meaning of adding a symbol before a function is unnecessary. we are all using it, that is, two variables point to an address at the same time, so chhua () is added before the php function () {static $ bwww: Decomposition of the function prefix & symbol in php. php function

You don't need to talk about the meaning of adding the & symbol in front of the php variable. everyone is using it, that is, two variables point to an address at the same time. so, what is the meaning of adding & symbol before the php function? Next, we will first demonstrate the code and then explain it again.

Function & chhua () {static $ B = "www.jb51.net"; // declare a static variable $ B = $ B. "WEB development"; echo $ B; return $ B;} $ a = chhua (); // This statement outputs the value of $ B as "www. jb51.netWEB development "$ a =" PHP "; echo"
"; $ A = chhua (); // This sentence will output the value of $ B as" www. jb51.netWEB development WEB development "echo"
"; $ A = & chhua (); // This statement outputs the value of $ B as" www. jb51.netWEB development WEB development "echo"
"; $ A =" JS "; $ a = chhua (); // This statement outputs the value of $ B as" JSWEB development "function & 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 a value of 6 for $ B.

Next, let's explain the second function.
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.


What is @ added before the php function name?

@ Operator is only valid for expressions. A simple rule for beginners is: if you can get a value from somewhere, you can add the @ operator before it. For example, you can place it before variables, functions, include () calls, constants, and so on. It cannot be placed before the definition of a function or class, nor can it be used for condition structures such as if and foreach.

The current "@" error control operator prefix even invalidates error reports that cause severe errors caused by script termination. This means that if "@" is used to suppress the error message before a function call that does not exist or has a type error, the script will not show any evidence of the cause and die there.

What is the meaning of adding & symbol before function definition in php?

Function Reference
The main difference between function reference and memory performance is ~
Function & load_message and function load_message. The first one must be declared as a reference and returned for use. The second one is to assign the result of the function to a variable, this function is generally used for recursion. As for references, we recommend that you write several examples and test them. As you can see, it is better to simply look at the literal meaning than practice. at the same time, there are many examples of php reference on the Internet. For more information, see.

By the way, there is also a common form of reference in php.
Function gao (& $ id) and function gao ($ id)
For this type of reference, we only need to compare the results.
Function a (& $ t ){
$ T. = 'XX ';
}
Function B ($ t ){
$ T. = 'yy ';
}

$ X = 'a ';
B ($ x );
Echo $ x; // output aa
A ($ x );
Echo $ x; // output aaxx
Remember that $ x is output here, not B ($ x) or a ($ x). (sorry, I borrowed the cosmicdusts example)

---- Learn from others'

Using () {static $ B = "www...

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.