Callback Functions
Part of the Logic & Flow section of Coddy's PHP journey — lesson 4 of 68.
A callback function is a function that gets passed as an argument to another function. Instead of calling the function immediately, you pass its name so the receiving function can call it later when needed.
To pass a function as a callback, you simply use the function's name as a string:
<?php
function greet($name) {
echo "Hello, " . $name . "!";
}
function processUser($username, $callback) {
// Do some processing...
$callback($username); // Call the callback function
}
processUser("Alice", "greet"); // Outputs: Hello, Alice!
?>In this example, "greet" is passed as a callback to processUser(). The processUser() function then calls the callback by using the string as a function name.
Callbacks are incredibly useful because they make your functions more flexible. The same function can perform different actions depending on which callback you pass to it. This concept is fundamental to many of PHP's built-in functions like array_map() and array_filter(), which you'll encounter later in this course.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate two functions: one called formatUppercase that takes a string and returns it in uppercase, and another called formatLowercase that takes a string and returns it in lowercase.
Then create a function called applyFormat that accepts two parameters: a text string and a callback function name. This function should apply the callback to the text and return the result.
You will receive two inputs: the operation type ("upper" or "lower") and the text to format. Read both inputs, determine which callback to use based on the operation type, call applyFormat with the appropriate callback, and print the formatted result.
Input format: Two lines - first line contains the operation type ("upper" or "lower"), second line contains the text to format
Expected output: The formatted text based on the operation type
Cheat sheet
A callback function is a function that gets passed as an argument to another function. Instead of calling the function immediately, you pass its name so the receiving function can call it later when needed.
To pass a function as a callback, use the function's name as a string:
<?php
function greet($name) {
echo "Hello, " . $name . "!";
}
function processUser($username, $callback) {
// Do some processing...
$callback($username); // Call the callback function
}
processUser("Alice", "greet"); // Outputs: Hello, Alice!
?>Callbacks make functions more flexible by allowing different actions depending on which callback you pass to them.
Try it yourself
<?php
// Read input
$operation = trim(fgets(STDIN));
$text = trim(fgets(STDIN));
// TODO: Write your code below
// Create formatUppercase function
// Create formatLowercase function
// Create applyFormat function
// Determine which callback to use based on operation type
// Call applyFormat with the appropriate callback
// Output the result
echo $result;
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting