Explain PHP Callback Functions
A callback function is a function that is passed as an argument to another function. In PHP, callback functions can be used in various contexts, such as sorting arrays, handling events, and more. Let's go through some examples to understand how callback functions work in PHP.
Example 1: Using Callback Functions with usort()
The usort()
function in PHP allows you to sort an array using a custom comparison function, which is passed as a callback.
<?php
// Custom comparison function
function compare($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$array = [3, 2, 5, 6, 1, 4];
// Sort the array using the custom comparison function
usort($array, 'compare');
print_r($array);
?>
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
In this example, the compare()
function is used as a callback to define the custom sorting logic for the usort()
function.
Example 2: Using Anonymous Functions as Callbacks
You can also use anonymous functions (or closures) as callbacks.
<?php
$array = [3, 2, 5, 6, 1, 4];
// Sort the array using an anonymous function as the comparison function
usort($array, function($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
print_r($array);
?>
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
In this example, an anonymous function is used directly within the usort()
call as the comparison function.
Example 3: Using Callback Functions with array_map()
The array_map()
function applies a callback function to each element of an array.
<?php
function square($n) {
return $n * $n;
}
$array = [1, 2, 3, 4, 5];
// Apply the square function to each element of the array
$result = array_map('square', $array);
print_r($result);
?>
Output:
Array
(
[0] => 1
[1] => 4
[2] => 9
[3] => 16
[4] => 25
)
In this example, the square()
function is used as a callback to apply the square operation to each element of the array.
Example 4: Using Object Methods as Callbacks
You can also use object methods as callbacks. For instance, if you have an object with a method that you want to use as a callback, you can do so by specifying the array format [object, method]
.
<?php
class MyClass {
public function myMethod($n) {
return $n * 2;
}
}
$obj = new MyClass();
$array = [1, 2, 3, 4, 5];
// Apply the myMethod function of the object to each element of the array
$result = array_map([$obj, 'myMethod'], $array);
print_r($result);
?>
Output:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
In this example, the myMethod()
function of the MyClass
object is used as a callback to apply the operation to each element of the array.
These examples demonstrate the versatility of callback functions in PHP and how they can be used in different scenarios to achieve various functionalities.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.