+

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

Example :  
// Function with no argument function func() { echo "geek" ; }  
// Function with an argument function fun ( $msg ) { echo $msg ; }  
// Call func and fun using $var and $var1 $var = " func " ; $var1 = "fun" ; ?>
There are two ways to do this. One - this is a direct function call by variable name using parentheses and parameters, and the other - using the call_user_func() function, but in both methods you must use the variable name.Program :  
// Function without an argument function func() { echo "hello" ; }  
// Function with an argument function fun ( $msg ) { echo $msg . "" ; } $var = "func" ; $var1 = "fun" ;  
// 1st method using variable name $var (); $var1 ( "geek" ); echo " " ;  
// 2nd method using built-in PHP
// function call_user_func() call_user_func ( $var ); call_user_func ( $var1 , "fun_function" );  
?>
Exit:
hello geek hello fun_function 
Another method: Using the eval() function: Function eval() - it is a built-in function in PHP that is used to evaluate a string as PHP code. 
Note:this method is provided by Vineet Joshi
Syntax:
eval ($code) 
Parameters:This function accepts one parameter code that used to store PHP code as a string.Example :  
// Function without argument function writeMessage() { echo "Welcome to GeeksforGeeks!" ; }  
// Declare a variable and store
// function name $functionName = "writeMessage();" ;  
// function call using eval eval ( $functionName ); ?>
Exit:
Welcome to GeeksforGeeks ! 
Get Solution for free from DataCamp guru