Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

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().

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page