What's the use of PHP anonymous functions?
Xiaobian to share with you what PHP anonymous function is used, I believe most people do not know how, so share this article for everyone's reference, I hope you have a lot of gains after reading this article, let's go to understand it together!
PHP anonymous functions
What is anonymous function?
Anonymous functions are also called closure functions, i.e. functions without a function name. PHP can create a function without a name, anonymous functions are mostly used in callback functions.
Anonymous functions can assign numbers directly to variables, anonymous functions are still functions, parameters can be passed, assign numbers directly to variables, and calling variables is calling functions.
The syntax for creating an anonymous variable is:
function (parameter list) { ...}
Anonymous functions are very similar to ordinary functions in that they can have a return value and accept arguments, except that anonymous functions omit the function name when declared.
Examples are as follows:
Output:
Next, look at two associative anonymous functions.
Anonymous function of variable functional expression
Output:
The function body in the above example has no function name, and is called by adding parentheses to $a. This is a variable functional anonymous function.
Callback anonymous functions
In our actual use, anonymous functions are often used as callbacks for functions or methods, not only callback functions, but also anonymous functions in many PHP internal functions. The example code is as follows:
Output:
The above is "PHP anonymous function what to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!