The so-called callback function means that when a function is called, a standard variable is not passed as a parameter, but another function is passed as a parameter to the called function. The so-called
Callback functionThat is, when a function is called, a standard variable is not passed as a parameter, but another function is passed as a parameter to the called function. In the format description of the function
CallbackType parameter, the function is the callback function. The previous section describes php variable parameter functions. This section describes php callback functions.
The format of the callback function is described as follows:
Mixed funName (callback arg) // use the pseudo-type callback description in the parameter list
Callback is also a pseudo type in PHP, indicating that the parameter of a function must take another function as a real parameter. A very important question is, why should we use functions as parameters? As mentioned above, passing parameters can change the execution behavior of the called function, but sometimes the ability to pass only one value to the function is limited. If a user-defined execution process can be passed to the function for use, the user's function extension is greatly increased. How to declare and use the callback function is also a key issue. if you need to declare the callback function, you need to first understand the variable function.
Variable functions
Variable functions are also called variable functions. If a variable name is followed by parentheses, PHP searches for a function with the same name as the value of the variable and tries to execute it. For example, declare a function test () and assign the function name string test to the variable $ demo. If you print the $ demo variable directly, the output value must be the string test. However, if you add parentheses $ demo () after the $ demo variable (), call the function corresponding to the $ demo variable value test. In this way, different function names can be assigned to the same variable, and then called through the variable, similar to the application of object-oriented polymorphism.
The instance code is as follows:
In the preceding example, the one (), two (), and three () functions are declared to calculate the sum, product, and operator of two numbers respectively. Then, assign the function names (without parentheses) of the three functions to the variable $ result as strings, add parentheses after the variable name $ result, and input two integer parameters, function execution with the same name as the value of the variable $ result will be searched. Most functions can assign function names to variables to form variable functions. However, variable functions cannot be used in the language structure.
Use variable function declaration and application callback function
If you want to customize a function that can be called back, you can use the variable function to help implement the function. When defining a callback function, the declared structure of the function remains unchanged, as long as the declared parameter is a common variable. However, when this parameter variable is applied inside a function, if you add parentheses, you can call a function with the same name as this parameter value, therefore, the parameter passed for it must be the name string of another function. The purpose of using the callback function is to upload a custom function to the function for internal use.
The code example is as follows:
';}} // Declare a function one. if the parameter is a multiple of 3, true is returned. otherwise, false function one ($ num) is returned) {return $ num % 3 = 0;} // declare a function two. if the parameter is a return value, true is returned. otherwise, false function two ($ num) is returned) {return $ num = strrev ($ num);} filter ("one"); // Print a multiple of not 3 within 10. the one parameter is the one () function () is a callback echo "---------------------
"; Filter (" two "); // Print the number of non-return files within 10. the parameter two is the name string of the function two (). Is it a callback?>
The running result is:
3. php video tutorial
The above are the callback functions of php functions (I) variable function definitions, declarations, and application details. For more information, see other related articles in the first PHP community!