tags:

views:

246

answers:

3

Hi im learning LISP and well, all day i program php for a living, so i was messing around with php.net and found the eval function... so i started playing around!

I would love to know more about how to use eval to do crazy stuff, i know you can make functions with this and everything... but i wanted to ask why the next code wont work:

$var = "echo \"FOZZ\";";
for($i = 0; $i < 100; $i++)
    $var  = "eval(\"".$var."\");";
print $var;   
eval($var);

Also what other stuff is interesting with eval!

+1  A: 

Your statement comes out to 'eval("eval("echo"FOZZ";");")'. Your double quotes are interfering with each other and causing the error.

You might want to try changin the first line to $var = "echo \'FOZZ\';";

Note: Please be careful using eval.

jacobangel
A: 

It won't work because you need to escape the quotes inside a quoted string. Since $var contains quotes, you need to escape the quotes at each iteration of stuffing it inside another layer of eval.

But regardless, I do not condone using eval() excessively like this. Using it even once should be rare and done only in the most necessary cases. eval() in any programming language is an accident waiting to happen.

Bill Karwin
I wouldn't say in _any_ language. But certainly in php.
troelskn
+1  A: 

For the common case of web programming Ive developed a library called xilla_tags that makes it easy to write lisp-flavoured PHP code, which can be much more readable that the traditional approach.

For xilla_tags see xilla tags sample.

Regarding lisp style in PHP, Jacob Hannsen has a great article on his bitchware site for a more general case.

gord