Why is the following code fails to compile:
<?php $str = "echo \"ok\""; $func = "eval"; $func ($str); ?> And php tells me eval was undefined.
In PHP, eval is a "language construct", not a function. (Notice It turns keyword-color in syntax highlighting editors.) Language constructs cannot be called using the variable function syntax $func() or used with other function-specific things like is_callable().
From http://www.php.net/manual/en/functions.variable-functions.php:
Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), include, require and the like. Utilize wrapper functions to make use of any of these constructs as variable functions.
eval() cannot be called using a variable function. If you don't understand what a variable function is? look at the following part of your code
$func = "eval"; $func ($str); //Here $func becomes eval upon execution making it a variable function If you look at the manual reference, its clearly noted
Note: Because this is a language construct and not a function, it cannot be called using variable functions.
eval is not a function its a language construct and variable functions won't work with language constructs.
You got Wrong..the code, maybe like example..
<?php $eval_code = '$my_name = "Swashata";' . '$your_name = "John";' . 'return $your_name;' . 'echo $my_name;'; $the_name = eval( $eval_code ); echo '<p>$the_name has got a value ' . $the_name . '</p>'; ?>
evalis not really a function, it's a language construct: "Note: Because this is a language construct and not a function, it cannot be called using variable functions." php.net/manual/en/function.eval.php<-(it's written in the docs).