PHP function Pre-add & symbol function decomposition, PHP function _php Tutorial

Source: Internet
Author: User

PHP function Pre-add & symbol function decomposition, PHP function


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 outputs $b value for "www.jb51.netWEB development" $a = "PHP"; echo "
"; $a =chhua ();//This statement outputs $b value of" Www.jb51.netWEB Developing Web Development " echo"
"; $a =&chhua ();//This statement outputs $b value of" Www.jb51.netWEB development Web Development Web development "echo"
"; $a =" JS "; $a =chhua (); This statement outputs the value of the $b "Jsweb development" function &test () {static $b =0;//declares a static variable $b= $b +1;echo $b; return $b;} $a =test ();// This statement will output the value of the $b 1$a=5, $a =test ();//This statement will output $b value of 2$a=&test ();//This statement will output a value of $b 3$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 the $b becomes 5.


What does it mean to precede the PHP function name with @?

The @ operator is valid only for an expression. A simple rule for beginners is that if you can get a value from somewhere, you can precede it with the @ operator. For example, you can put it in variables, functions and include () calls, constants, and so forth. It cannot be placed before the definition of a function or class, nor can it be used for conditional structures such as if and foreach.

The current "@" error control operator prefix even invalidates the fatal error report that caused the script to terminate. This means that if "@" is used to suppress the error message before a function call that does not exist or is of the wrong type, the script will die there without any indication of why.

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

for a reference to the functions, the main difference is in memory performance ~
Function &load_message and function Load_message, and the first one is used The &load_message declaration is returned as a reference, and the second one simply assigns the result of the function to a variable, which is typically used for recursion purposes. As for references, it is recommended that you write a few examples yourself and test them. I understand that it is not as good as practice to look at the literal meaning, and there are many examples of PHP references on the web. You can refer to it.

by the way, in the PHP reference, there is a common form of
is the function Gao (& $id) and function Gao ($id)
This form of reference, we just need to compare the results.
Function A (& $t) {
$t. = ' xx ';
}
Function B ($t) {
$t. = ' yy ';
}

$x = ' AA ';
B ($x);
Echo $x;//Output AA
A ($x);
echo $x; Output Aaxx
Remember that the output here is $x not B ($x) or a ($x). Sorry to borrow the cosmicdusts example)

----draw on someone else's
 

http://www.bkjia.com/PHPjc/840757.html www.bkjia.com true http://www.bkjia.com/PHPjc/840757.html techarticle PHP functions in front of the symbol is not to say, we are all in use, is two variables at the same time point to an address, then, the PHP function before adding Chhua () {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.