Skip to content

Callback

A callback is a function passed as an argument to another function.

Callbacks allow functions to execute custom logic.

They are commonly used with array functions and event handlers.

function greet($name) {
echo "Hello $name";
}
function processUser($callback) {
$callback("John");
}
processUser("greet");
processUser(function ($name) {
echo "Welcome $name";
});
$numbers = [1,2,3,4];
$result = array_map(function ($n) {
return $n * 2;
}, $numbers);
print_r($result);