0

Why is the following code fails to compile:

<?php $str = "echo \"ok\""; $func = "eval"; $func ($str); ?> 

And php tells me eval was undefined.

2
  • 7
    Because eval is 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). Commented Oct 28, 2012 at 7:43
  • @Asad thanks to starx's answer, I finally know what "evaling eval" means in your comment ;-P right, that's what I'm doing Commented Oct 28, 2012 at 7:54

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

eval is not a function its a language construct and variable functions won't work with language constructs.

Comments

-1

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>'; ?> 

1 Comment

So... what's the big difference in your code? What is wrong with the OP's code? Why does it fail?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.