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 

<!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: 2026-03-11T22:50:52+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements