Why is this PHP program not working?

I am new to PHPand learned about features PHPfrom w3schools. He said: "PHP allows a function call 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?

+5
source share
4 answers

This function PHPis called Function Variables .

echo, , . var_dump , .

PHP doc :

, echo(), print(), unset(), isset(), empty(), include (), require() .. , .

printf echo :

$e = "printf"; // printf is a function not a language construct.
$e('foo');

echo :

$e = "echo_wrapper";
$e('foo');

function echo_wrapper($input) { // wrapper function that uses echo.
        echo $input;
}
+15

echo ! printf, , . ​​

+1

, :

  • This function only works in PHP 5.3, as far as I remember. This is the newest major version, so you should make sure to use it. It is very likely that you did not.
  • echonot a function, but rather a construction of the PHP language. You need to write a wrapper function that echoespassed to it.
0
source

It works:

$v = "printf";
$v('foo');
0
source

All Articles