← Back to PHP Course | Chapter 5: Functions | Lesson 10 of 10

PHP Callback Functions

A callback is like giving a friend your phone number and saying 'call me when you find it'. You hand a function to another function so it can run yours at the right moment.
Syntax
php
$result = array_map("function_name", $array);            // function name as string
$result = array_map(fn($x) => expression, $array);      // closure
$result = array_filter($array, "function_name");

What Is a Callback?

A callback is any value PHP can call as a function when passed into another function -- a string naming a function, a closure, or an array pairing an object with a method name.

Callbacks let you hand custom behavior into generic array/sorting functions without them needing to know your logic in advance.

Example: What Is a Callback?

php
<?php
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
// Declare `$result`, set to `array_map("strval", $numbers)`
$result = array_map("strval", $numbers);
// Print a human-readable dump of `$result`
print_r($result);
?>

Passing a Function Name

The simplest callback is just the function's name as a string, like "strtoupper" passed to array_map().

PHP looks up that name at call time and invokes it, which works for both built-in and user-defined functions, but offers no type-checking until the call actually happens.

Example: Passing a Function Name

php
<?php
// Declare `$words` as an array: `["hello", "world"]`
$words = ["hello", "world"];
// Print a human-readable dump of `array_map("strtoupper", $words)`
print_r(array_map("strtoupper", $words));
?>

Passing a Closure

An anonymous function (function($x) { ... }) or arrow function (fn($x) => ...) defined inline is the most common modern callback style, since it lets you write the logic right where it's used instead of naming a separate function elsewhere.

Closures can also capture outer variables via use(), which named functions can't do.

Example: Passing a Closure

php
<?php
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
// Declare `$doubled`, set to `array_map(function ($n) { return $n * 2; }, $numbers)`
$doubled = array_map(function ($n) { return $n * 2; }, $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>

Passing a Method Callback

To use an object's method as a callback, pass a two-element array: [$object, methodName] for an instance method, or [ClassName, methodName] for a static one.

This is how you plug object behavior into functions like usort() without writing a wrapper closure.

Example: Passing a Method Callback

php
<?php
// Define the class `Formatter`
class Formatter {
    // Define the function `upper` taking `$s`
    function upper($s) { return strtoupper($s); }
}
// Create a new `Formatter` instance, stored in `$formatter`
$formatter = new Formatter();
// Declare `$result`, set to `array_map([$formatter, 'upper'], ["hello", "world"])`
$result = array_map([$formatter, 'upper'], ["hello", "world"]);
// Print a human-readable dump of `$result`
print_r($result);
?>

Common Callback Consumers

array_map() applies a callback to every element and returns a new array, array_filter() keeps only elements the callback returns true for, and usort() uses a callback that returns negative/zero/positive to define a custom sort order.

All three expect the exact same callback formats covered above.

Example: Common Callback Consumers

php
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4]`
$numbers = [1, 2, 3, 4];
// Print a human-readable dump of `array_map(fn($n) => $n * 2, $numbers)`
print_r(array_map(fn($n) => $n * 2, $numbers));
// Print a human-readable dump of `array_filter($numbers, fn($n) => $n % 2 === 0)`
print_r(array_filter($numbers, fn($n) => $n % 2 === 0));
// Call `usort($numbers, fn($a, $b) => $b <=> $a)`
usort($numbers, fn($a, $b) => $b <=> $a);
// Print a human-readable dump of `$numbers`
print_r($numbers);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Passing a function name without quotes, like array_map(strtoupper, $a), which PHP reads as an undefined constant.
  2. Writing [$obj, method] with a private method, which fails because the callable cannot access it from outside.
  3. Forgetting that array_map callbacks receive only values, while array_filter with ARRAY_FILTER_USE_KEY receives keys, so arguments differ between functions.
Chapter Summary
  • Functions take parameters and default arguments, and return values.
  • Variable functions, anonymous functions, arrow functions, and callbacks treat functions as values.
  • Recursion and scope decide how functions repeat and which variables they see.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.