mixed eval ( string code )

You can execute the contents of a string as if it were PHP code using the eval() function. This takes just one string parameter and executes that string as PHP. For example:

    $str = '$i = 1; print $i;';

That script assigns two PHP statements to $str, then passes $str into eval() for execution.

The eval() function allows you to store your PHP code in a database, or to build it at runtime, which gives you a lot more flexibility.


If you are considering using eval(), bear in mind these words from the creator of PHP, Rasmus Lerdorf: “If eval() is the answer, you’re almost certainly asking the wrong question.” That is, you should be able to achieve your goals without resorting to eval().

Get PHP in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.