The callback function of PHP function (ii) Call_user_func_array () function

Source: Internet
Author: User
Tags php programming
callback functionPasses a function as an argument to the called function. If a parameter of type callback is present in the format description of the function, the function is the callback function. Although variable functions can be used to declare their own callback functions, most of them are implemented by the Call_user_func_array () function. The previous chapter introduces the variable function declaration and application in the PHP callback function, using variable function to pass a user-defined execution procedure to the function, which greatly increases the user's extension to function function, and passes the function of one's own definition to the function internal use. This section describes the next Call_user_func_array () function as an example.

The function Call_user_func_array () is a built-in function in PHP, but it is also a callback function with the following format:

Mixed Call_user_func_array (callback function, array Param_arr)

The function has two parameters: the first parameter requires a string to represent the function name to invoke because it uses a pseudo-type callback, and the second parameter is an array-type parameter that represents the parameter list, which is passed to the function to be called once in a straight pass. Declares a function, which is output, and is intended to function as a callback function.

The application example for this function is as follows:

<?php//Declare a function fun (), function is to output two strings, the purpose is as the Call_user_func_array () function of the callback function fun ($str 1, $str 2) {   echo "$str 1 =". $str 1;   echo "<br>";   echo "$str 2 =". $str 2;} Call function Fun () by system function Call_user_func_array () Call_user_func_array ("Fun", Array ("php.cn", "PHP Chinese web")); >

In the above example, the function fun () is called by the system function Call_user_func_array (), and the name string of the function fun () is passed to the first parameter in the Call_user_func_array () function. The second parameter requires an array, and the number of elements in the array must be the same as the parameter list and tree of the fun () function. Because each element value in this array parameter is passed to the callback to the function fun () parameter list, in order, through the Call_user_func_array () function. So we can change the custom callback function, which is implemented by the variable function, into the way of the Call_user_func_array () function.

An example of its code is shown below:

<?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 + +) {     // Use the system function Call_user_func_array (), call the same function as the variable $fun value     if (Call_user_func_array ($fun, Array ($i)))       continue;     echo $i. ' <br> ';   } }?>

In this example, the Custom function filter () internally overwrites the original variable function position with the use of the Call_user_func_array () function, and the application of the function filter () has not changed.

This chapter explains the Call_user_func_array () function of the PHP callback function and its usage, and the next chapter introduces the class static function of the PHP callback function and the method callback of the object.

"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

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.