How to call a function from a string stored in a variable with PHP?

Spread the love

Sometimes, we want to call a function from a string stored in a variable with PHP.

In this article, we’ll look at how to call a function from a string stored in a variable with PHP.

How to call a function from a string stored in a variable with PHP?

To call a function from a string stored in a variable with PHP, we can use the variable name as the function name.

For instance, we write

function foo ()
{
    //...
}

function bar ()
{
    //...
}

$functionName = "foo";
$functionName()

to call foo with $functionName().

Conclusion

To call a function from a string stored in a variable with PHP, we can use the variable name as the function name.

Leave a Reply

Your email address will not be published.