Name
eval()
Synopsis
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.
Warning
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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.