How to write php callback function

silencement
Release: 2023-02-24 18:08:01
Original
3676 people have browsed it

How to write php callback function

The definition and calling of ordinary functions are similar to js. This definition method does not require a return value, even if there is a return value, there is no need to add it when declaring.

In later PHP versions, a very useful feature was added, which is that it can be called before the function is defined.

echo add(1,2);
echo "
"; function add($a,$b){ return $a+$b; } function sub($a,$b){ return $a-$b; } echo add(23,12); echo "
"; echo sub(23,22); echo "
";
Copy after login

The following is a very useful function, which is the variable function. As the name suggests, the function is used as a variable.

The advantage is that different functions can be called with the same variable, which is very similar to polymorphic calling of functions.

$var="add";
echo $var(4,2);
echo "
"; $var="sub"; echo $var(4,2); echo "
";
Copy after login

The callback function is to pass a process to a function when passing a simple parameter cannot solve the problem, so as to achieve the purpose.

Passing a function as a parameter when calling a function is a function callback.

$arr=array(2,3,5,4,1,6,7,9,8);
var_dump($arr);
echo "
";
Copy after login
//这里是自定义回调函数,返回-1是指将两个元素交换,0和1是不发生改变。
function myrule($a,$b){
    if ($a>$b){
        return 1;
    }
    elseif ($a<$b){
        return -1;
    }
    else{
        return 0;
    }
}
//usort就是系统函数,但是他的第二个参数是回调函数,这个函数参数就是排序规则
usort($arr,"myrule");
var_dump($arr);
echo "
";
Copy after login

Write your own callback function and use variable functions and callbacks to complete a simple filtering condition. If multiple conditions need to be satisfied at the same time, just give it an && relationship.

The variable function used can be called using a function called call_user_func_array() in the system. It has two parameters: the name of the callback function and the parameter array

call_user_func_array("demo ", array(1,3)); Its advantage is that the number of parameters in array can be less than that of the original function, even when there are default parameters.

//rule1除去数组中是三的倍数的数
function rule1($a){
    if ($a%3==0){
        return true;
    }else{
        return false;
    }
}
//rule2是除去数组中的回文数(从左边读与从右边读是一样的)
function rule2($a){
    if ($a==strrev($a)){
        return true;
    }else{
        return false;
    }
}
function demo($n,$var){
    for ($i=0;$i<$n;$i++){
        if (call_user_func_array($var,array(23)))
        //if ($var($i))
        {
            continue;
        }else{
            echo $i."
"; } } } $var="rule1"; demo(100,$var); echo "
"; echo "
"; $var="rule2"; demo(200,$var); echo "
";
Copy after login

Note that when calling a method in an object, we need to pass in an anonymous object and then specify the function name

. When calling a static method in a class, we only need to specify the class name.

In the above two cases, the variable function cannot be completely used. You can only use the system callback call_user_func_array(), which is just a variable function to pass parameters without calling

class A{
    function one(){
    }
    static function two(){
    }}demo(200,array(new A,"one"));demo(200,array("A","two"));
Copy after login

The above is the detailed content of How to write php callback function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!