Calling a Function by Name in PHP
Let's say you have a string with a function name stored in a variable. Using this variable, you can call the function whose name is stored in that variable.
Let's look at an example. Suppose we have the following function:
<?php
function func($num)
{
echo $num ** 2;
}
?>
Let's also assume we have a variable with the name of this function:
<?php
$name = 'func';
?>
Let's call the function by its name. To do this, we write the variable containing the function name and put parentheses for the call after it:
<?php
$name(3); // 9
?>
Suppose the variable stores the name
of the built-in PHP function sqrt
for finding the square root:
<?php
$name = 'sqrt';
?>
Call this function using the variable with its name.