Php and macros (lisp style)?
Hi I was learning LISP and well, all day I was programming php for life, so I fiddled with php.net and found the eval function ... so I started playing!
I would love to know more about how to use eval to do crazy things, I know you can do functions with this and all ... but I wanted to ask why the following code won't work:
$var = "echo \"FOZZ\";";
for($i = 0; $i < 100; $i++)
$var = "eval(\"".$var."\");";
print $var;
eval($var);
And what's even more interesting with eval!
source to share
For the common web programming case, Ive developed a library called xilla_tags that makes it easier to write lisp-flavored PHP code that can be much more readable than the traditional approach.
For xilla_tags see the xilla tag sample .
Regarding the lisp style in PHP, Jacob Hannsen has a great article on his bitch website for a more general case.
source to share
It won't work because you need to escape the quotes inside the quoted string. Since it $var
contains quotes, you need to escape the quotes on each iteration by stuffing it in a different eval layer.
Despite this, I do not tolerate overuse eval()
. Using it even once should be rare and only done when absolutely necessary. eval()
in any programming language there is an accident waiting to exist.
source to share