I’m new to PHP and was learning about PHP functions from w3schools. It said “PHP allows a function call to be made when the function name is in a variable”
This program worked
<?php
$v = "var_dump";
$v('foo');
?>
But this program did not work:
<?php
$v = "echo";
$v('foo');
?>
But if I do echo('foo'); it works.
What am I doing wrong?
This feature of
PHPis called Variable functions.The issue here is with
echowhich is not really a function but a language construct and variable functions can only be used with functions. In your first examplevar_dumpwas a function and it worked fine.From PHP doc for Variable functions:
You can make use of
printffunction in place ofechoas:or you can write a wrapper function for
echoas: