October 2005
Intermediate to advanced
372 pages
11h 35m
English
eval()
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;';
eval($str);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().