Variable Functions
Part of the Logic & Flow section of Coddy's PHP journey — lesson 6 of 68.
Variable functions offer another way to call functions dynamically in PHP. Instead of using call_user_func(), you can directly call a function by treating a variable containing the function name as if it were the function itself.
The syntax is simple: if you have a variable containing a function name, you can call that function by adding parentheses to the variable:
<?php
function sayHello() {
echo "Hello World!";
}
$functionName = "sayHello";
$functionName(); // Outputs: Hello World!
?>This works with functions that accept parameters too. You simply pass the arguments inside the parentheses:
<?php
function greet($name) {
echo "Hello, " . $name . "!";
}
$func = "greet";
$func("Alice"); // Outputs: Hello, Alice!
?>Variable functions provide a clean and intuitive way to execute code dynamically. This feature is particularly useful when you need to call different functions based on conditions or user input, making your code more flexible and adaptable.
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: calculateArea that takes two numbers (length and width) and returns their product, calculatePerimeter that takes two numbers and returns twice their sum, and calculateDiagonal that takes two numbers and returns the square root of the sum of their squares.
You will receive three inputs: the operation name ("area", "perimeter", or "diagonal"), the length, and the width. Read all three inputs, store the function name in a variable, call that function using the variable function syntax with the two measurements, and print the result.
Input format: Three lines - first line contains the operation name, second line contains the length (a number which may contain decimals), third line contains the width (a number which may contain decimals)
Expected output: The result of the calculation
Cheat sheet
Variable functions allow you to call functions dynamically by treating a variable containing the function name as the function itself.
Basic syntax - add parentheses to a variable containing a function name:
<?php
function sayHello() {
echo "Hello World!";
}
$functionName = "sayHello";
$functionName(); // Outputs: Hello World!
?>Variable functions work with parameters too:
<?php
function greet($name) {
echo "Hello, " . $name . "!";
}
$func = "greet";
$func("Alice"); // Outputs: Hello, Alice!
?>Try it yourself
<?php
// Read inputs
$operation = trim(fgets(STDIN));
$length = floatval(fgets(STDIN));
$width = floatval(fgets(STDIN));
// TODO: Create the three functions here (calculateArea, calculatePerimeter, calculateDiagonal)
// TODO: Call the appropriate function using variable function syntax and store the result
// 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