PHP Callback Functions
Callback Functions
A callback function is a function that passes as an argument into another function (commonly referred to as "callback").
Callback function Any existing function can be used. In order to use a function as a callback feature, use a string that contains another function's name :
Example 1 :- To calculate each string in a table, pass a callback to the PHP array_map()
method :
Output :-
(
[0] => 5
[1] => 6
[2] => 6
[3] => 7
)
Example 2 :- As a callback to PHP's array_map()
function, use an anonymous function:
Output :-
(
[0] => 5
[1] => 6
[2] => 6
[3] => 7
)
Related Links
Callbacks in User Defined Functions
Callback functions can also be passed as arguments to user-defined functions and procedures. Call callback functions inside a user-defined function or method by enclosing the variable in parentheses and passing arguments as you would with regular functions :
Example :- Call a user-defined function with a callback :
";
}
function print2($str) {
return $str . "?
";
}
function myPrint($str, $format) {
// Calling the $format callback function
echo $format($str);
}
// Pass "print1" and "print2" as callback functions to "myPrint".
myPrint("Hello world", "print1");
myPrint("Hello world", "print2");
?>
Output :-
Hello world?
Related Links