Called
callback function, which means that the calling function is not passing a standard variable as a parameter, but instead passes another function as an argument to the called function. If it appears in the format description of the function
CallbackType, the function is the callback function. The previous section describes the functions of the PHP variable parameter, which goes into the PHP callback function learning.
The format of the callback function is described as follows:
Mixed Funname (callback arg) //Use pseudo type callback description in parameter list
Callback is also part of a pseudo-type in PHP, stating that a function's arguments need to accept another function as an argument. A very important question is why use functions as parameters? As explained earlier, the execution behavior of the calling function can be changed by passing parameters, but sometimes it is limited to pass only one value to the function. If you can pass a user-defined execution procedure to a function, it greatly increases the user's ability to extend functions. And how to declare and use callback functions is also a key issue, if you need to declare a callback function, you need to understand the variable function first.
Variable function
Variable functions are also known as mutable functions. If a variable name is followed by parentheses, PHP will look for a function with the same name as the value of the variable and will attempt to execute it. For example, declare a function test () and assign the function name string test to the variable $demo. If the $demo variable is printed directly, the output value must be a string test, but if the $demo variable is followed by parentheses $demo (), the function that corresponds to the $demo variable value test is called. This allows you to assign different function names to the same variable, and then call the function through the variable, similar to the application of the polymorphic attribute in the object-oriented.
In fact the example code is as follows:
<?php function One ($a, $b) { return $a + $b;} function one ($a, $b) { return $a * $b;} function three ($a, $b) { re Turn $a/$b; } $result = "one"; Assigns the function name one to the variable $result, and executes the $result () call functions One ()//$result = "two"; The function name two is assigned to the variable $result, and $result () is called when it is executed ()//$result = "three"; The function name three is assigned to the variable $result, and when the $result () is executed, the three () echo "result is:". $result (4,6); The variable $result the value of which function name is received, which function is called?>
In the above example, one (), two (), and three () three functions are declared, respectively, for calculating the sum, product, and quotient of two numbers. The function name of the three function (without parentheses) is assigned to the variable $result as a string, and then using the variable name $result followed by parentheses and passing in two integer parameters, a function execution with the same name as the value $result of the variable is sought. Most functions can assign a function name to a variable and form a variable function. However, variable functions cannot be used for language structures.
Declaring and applying callback functions using variable functions
If you want to customize a function that can be recalled, you can choose to use the variable function to help implement it. When defining a callback function, the declaration structure of the function is unchanged, as long as the declared argument is a normal variable. But when the parameter variable is applied inside the function, it can be called to a function with the same name as the parameter value, so the argument passed to it must be the string of the name of the other function. The purpose of using a callback function is to pass a section of your own definition to the function for internal use.
Its code example is as follows:
<?php//Declaration callback function filter, in the integer 0-10 by the custom condition filtering does not have the number function filter ($fun) {for ($i =0; $i <=10; $i + +) { //parameter variable $ Fun plus a parenthesis $fun (), the function if ($fun ($i)) with the same name as the call and the variable $fun value continue; echo $i. ' <br> '; } }//Declare a function one, if the argument is a multiple of 3 to return ture, otherwise return false function one ($num) { return $num%3 = = 0;}//Declare a function of both, if the parameter is a palindrome number return ture, Otherwise false function ($num) { return $num = = Strrev ($num);} filter ("one"); Prints a multiple of 10 not 3, and the parameter one is the name string of the function A (), which is a callback echo "---------------------<br>"; Filter ("both"); Print out 10 non-palindrome number, parameter is the name string of function one (), is a callback?>
The result of the operation is:
In the above example, if the declared function filter () simply accepts a normal value as a parameter, the number that the user can filter out is relatively single. In this example, in the definition of the function filter () Called to pass through the parameters of a tax-included as a filter, so that the function is much more powerful, you can filter in the filter () function of any number you do not like. In the function filter (), you can call a function that has the same value as the variable $fun by using the parameter variable $fun plus a parenthesis $fun () as the filter condition. For example, in this example, one () and two () two functions are declared, respectively, to filter out the multiples of 10 and the palindrome number, as long as the function name, a and two strings are passed to the parameter when the filter () is called, and both functions are passed to the filter () function for internal use 。
This chapter explains the variable function definition, declaration, and application of the PHP callback function, and the next chapter introduces the Call_user_func_array () function of the PHP callback function and its usage.
"Related tutorials Recommended"
1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"
2. PHP programming from getting started to mastering the full set of video tutorials
3. PHP real-Combat video tutorial