PHP Learning notes variable variables, variable functions, and anonymous functions in 5--php

Source: Internet
Author: User


Variable variables refer to the value of a variable as a variable name again to get the value of another variable.
Such as:
$name = ' DQRCSC ';
$myname = ' name ';//The value of the $myname happens to be the variable name of another variable
echo $name;//output $name value ' DQRCSC '
echo $myname;//output $myname value ' name '
echo $ $myname;//Get $myname value ' name ', parse it into a variable $name, and output $name value ' DQRCSC '

anonymous function: That is, a function without a name
The definition of a function in PHP is stored in the code area by the system in the compile phase, and the function's code can be found in the code area by the function name.
If you do not have a name, you need to save the memory address of the function through a variable.
$func = function () {
echo ' Test ';
};
Var_dump ($func);//object (Closure) #1 This is a closure.
Save the address of the function, and then how to invoke the anonymous function?
Recall the invocation form of the function: MyFunc (), followed by a pair of parentheses after the function name, indicating that the function was called. Anonymous function calls in the same form
$func ();//indicates the anonymous function to which the variable is called
Since the addition of () is considered a calling function, the concept of mutable function is naturally present here.

mutable functions: Like mutable variables, a variable holds the name of the function, obtains the function's value as the function's name, and then interprets it as a function.
Such as:
function Test () {
echo ' Test ';
}
$func = ' Test ';
$func ();//$func Gets the value of the variable ' test ', followed by (), is called as the function test ().

What if the anonymous function?
$func = function () {
$name = ' DQRCSC ';
Echo $name;
};
$myfunc = ' func '; $myfunc is a mutable variable that stores the variable name of the $func
$ $myfunc ();//$ $myfunc parse variable variable, get $func value, followed by (), as function to call, then output ' DQRCSC '

From the above, mutable variables and variable functions are the same principle, but the resolution is different, a variable want to parse it into a variable variable, plus a $ symbol,
You want to parse it into a mutable function, followed by ().
So the question is, can functions and variables have the same name? The answer is yes.
function Test () {
echo ' function ';
}
$test = ' var ';//variable with the same name as the function
$myvar = ' test ';//define a variable that holds exactly the value of the variable name and function name
echo $ $myvar;//parse it into mutable variable, output ' var '
$myvar ();//parse it into variable function, output ' function '

PHP Learning notes variable variables, variable functions, and anonymous functions in 5--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.