Menu
Coddy logo textTech

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.

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

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

All lessons in Logic & Flow