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.
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 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;
?>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