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

PHPServer Side ProgrammingProgramming

To call a function from a string stored in a variable, use $func.

The syntax is as follows 

$func=’anyFunctionName’;

Example

The PHP code is as follows 

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
function hello(){
   echo "Calling hello method."."<br>";
}
function welcome(){
   echo "Calling welcome method."."<br>";
}
$func = 'hello';
$func();
?>
</body>
</html>

Output

This will produce the following output

Calling hello method.
raja
Published on 13-Oct-2020 08:02:39
Advertisements