PHP Callback Functions (original) (raw)

Last Updated : 15 Apr, 2025

In PHP, the callback functions are related to the dynamic behavior and flexibility in code execution. They are used to pass custom logic or functions as arguments to other functions, letting developers change how a function behaves without changing its main structure. This makes the code more reusable, modular, and adaptable to different situations.

In this article, we will discuss PHP callback functions, their types, and how they are used.

What is a Callback Function in PHP?

A callback function is simply a function that you can pass as an argument to another function and invoke at a later time. In PHP, callbacks are most commonly used when you need to allow dynamic behavior for functions, allowing users or other functions to determine what specific code should run.

**A callback function can be:

**Now, let us understand with the help of the example:

PHP `

callback(callback(callback(name); } processUser('greet'); ?>

`

**In this example:

Types of Callback Functions

Below are the following types of Callback functions in PHP.

1. Simple Callback Function

In the simplest form, you can pass the name of a function as a string to another function, and that function will be called when the callback is invoked.

PHP `

number∗number * numbernumber; } function applyCallback($numbers, $callback) { foreach ($numbers as $number) { echo callback(callback(callback(number) . "\n"; } } $numbers = [1, 2, 3, 4, 5]; applyCallback($numbers, 'square'); ?>

`

**In this example:

2. Object Method Callback

In object-oriented PHP, you can pass the method of an object as a callback by using an array format. This allows you to dynamically choose which method to call.

PHP `

callback(callback(callback(name); } $greeter = new Greeter(); processUserMethod([$greeter, 'greet']); ?>

`

**In this example:

3. Anonymous Function Callback

An anonymous function can be used as a callback in situations where you need to define a function on the fly, without declaring it beforehand.

PHP `

callback(callback(callback(name); } processUserAnonymous(function($name) { return "Hello, " . $name . "!"; }); ?>

`

**In this example:

Useful Functions for Callback Operations

PHP provides some built-in functions to work with callbacks more effectively. Here are a few:

1. call_user_func()

This function allows you to call a callback function passed as a string or an array.

PHP `

`

**In this example, call_user_func() is used to invoke the function sayHello() with the parameter ‘GFG. ‘

2. call_user_func_array()

This function is similar to call_user_func(), but it allows you to pass multiple arguments as an array.

PHP `

a+a + a+b; } echo call_user_func_array('sum', [10, 20]); ?>

`

**In this example, call_user_func_array() is used to pass the arguments [10, 20] to the function sum(), which will return 30.

3. array_map()

array_map() is a built-in PHP function that applies a callback to each element of an array.

PHP `

doubleNumbers=arraymap(function(doubleNumbers = array_map(function(doubleNumbers=arraymap(function(n) { return $n * 2; }, $numbers); print_r($doubleNumbers); ?>

`

Output

Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 )

**In this example, array_map() applies the anonymous function to each element of the array $numbers, doubling each number.

Advantages of Callback Functions