Menu
Coddy logo textTech

Using 'call_user_func'

Part of the Logic & Flow section of Coddy's PHP journey — lesson 5 of 68.

The call_user_func() function provides a powerful way to call functions dynamically in PHP. Instead of calling a function directly by its name, you can store the function name in a string variable and use call_user_func() to execute it.

Here's the basic syntax:

<?php
function greet() {
    echo "Hello World!";
}

$functionName = "greet";
call_user_func($functionName);  // Outputs: Hello World!
?>

This becomes especially useful when you need to call different functions based on user input or conditions. You can also pass arguments to the function by including them as additional parameters:

<?php
function add($a, $b) {
    return $a + $b;
}

$operation = "add";
$result = call_user_func($operation, 5, 3);
echo $result;  // Outputs: 8
?>

This dynamic function calling is particularly valuable when building flexible applications where the specific function to execute is determined at runtime, such as calculators, command processors, or routing systems.

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 three functions: multiply that takes two numbers and returns their product, subtract that takes two numbers and returns their difference, and divide that takes two numbers and returns their quotient.

You will receive three inputs: the operation name ("multiply", "subtract", or "divide"), and two numbers. Read all three inputs, use call_user_func() to execute the appropriate function with the two numbers, and print the result.

Input format: Three lines - first line contains the operation name, second line contains the first number, third line contains the second number

Expected output: The result of the operation

Cheat sheet

The call_user_func() function allows you to call functions dynamically using a string variable containing the function name:

<?php
function greet() {
    echo "Hello World!";
}

$functionName = "greet";
call_user_func($functionName);  // Outputs: Hello World!
?>

You can pass arguments to the function by including them as additional parameters:

<?php
function add($a, $b) {
    return $a + $b;
}

$operation = "add";
$result = call_user_func($operation, 5, 3);
echo $result;  // Outputs: 8
?>

Try it yourself

<?php
// Read input
$operation = trim(fgets(STDIN));
$num1 = floatval(fgets(STDIN));
$num2 = floatval(fgets(STDIN));

// TODO: Create the three functions (multiply, subtract, divide) here

// TODO: Use call_user_func() to execute the appropriate function

// 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