Menu
Coddy logo textTech

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.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create 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;
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow