← Back to PHP Course | Chapter 5: Functions | Lesson 5 of 10

PHP Variable Functions

A variable function is like keeping a phone number in a note and dialing whatever it says. PHP reads the text in the variable and calls the function that has that name.
Syntax
php
$function_name = "existing_function";
$function_name($argument);            // calls existing_function()
$object->$method_name($argument);     // variable method
ClassName::$method_name();            // variable static method

Introduction to Variable Functions

If a variable holds a string and you append () to it — $fn() — PHP looks for a function whose *name* matches that string's current value and calls it.

This indirection is what lets you decide, at runtime, which of several functions to invoke based on data rather than hardcoded logic.

Example: Introduction to Variable Functions

php
<?php
// Define the function `sayHello` with no parameters
function sayHello() {
    // Print "Hello!" to the output
    echo "Hello!";
}
// Declare `$fn`, set to "sayHello"
$fn = "sayHello";
$fn();
?>

Variable Object Methods

The same mechanism extends to object methods: storing a method name as a string in a variable and calling $object->$methodName() invokes that method dynamically, without the calling code needing to hardcode which specific method to run.

Example: Variable Object Methods

php
<?php
// Define the class `Greeter`
class Greeter {
    // Define the function `hello` with no parameters
    function hello() {
        // Print "Hi there!" to the output
        echo "Hi there!";
    }
}
// Create a new `Greeter` instance, stored in `$obj`
$obj = new Greeter();
// Declare `$methodName`, set to "hello"
$methodName = "hello";
$obj->$methodName();
?>

Variable Static Methods

You can dynamically call a class's static method the same way, by holding either the class name, the method name, or both as strings and combining them at call time — useful for dispatch tables that route to different handlers based on runtime data.

Example: Variable Static Methods

php
<?php
// Define the class `Math`
class Math {
    // Define the function `square` taking `$n`
    static function square($n) {
        // Return `$n * $n` from this function
        return $n * $n;
    }
}
// Declare `$class`, set to "Math"
$class = "Math";
// Declare `$method`, set to "square"
$method = "square";
// Print `$class::$method(4)` to the output
echo $class::$method(4);
?>

Checking Before Calling

Before calling anything this way, check is_callable($value) first.

Attempting to invoke a string that doesn't actually correspond to any real function or method throws a fatal error rather than failing gracefully, so validating first prevents a crash from a bad or mistyped name.

Example: Checking Before Calling

php
<?php
// Declare `$fn`, set to "strtoupper"
$fn = "strtoupper";
// Check whether `is_callable($fn)`
if (is_callable($fn)) {
    // Print `$fn("hello")` to the output
    echo $fn("hello");
}
?>

Variable Functions in Callbacks

Passing a string function name to something like array_map() or usort() as a callback relies on exactly this variable-function mechanism under the hood — PHP resolves the string to an actual function to call for each element, without you writing an explicit dispatch step yourself.

Example: Variable Functions in Callbacks

php
<?php
// Define the function `double` taking `$n`
function double($n) {
    // Return `$n * 2` from this function
    return $n * 2;
}
// Declare `$numbers` as an array: `[1, 2, 3]`
$numbers = [1, 2, 3];
// Declare `$doubled`, set to `array_map("double", $numbers)`
$doubled = array_map("double", $numbers);
// Print a human-readable dump of `$doubled`
print_r($doubled);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling a variable function whose name does not exist, like $fn = nofunc; $fn();, which causes a fatal 'Call to undefined function' error.
  2. Building the function name from user input, which lets visitors run any function; check against an allow-list.
  3. Trying to call language constructs like echo or print through a variable, which does not work because they are not real functions.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.