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


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.

Updated on: 13-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements