PHP 和宏(lisp 风格)?
你好,我正在学习 LISP,好吧,我整天以编程 php 为生,所以我在 php.net 上闲逛,发现了 eval 函数......所以我开始玩了!
我很想知道更多关于如何使用 eval 来做疯狂的事情,我知道你可以用这个和所有东西来创建函数......但我想问为什么下一个代码不起作用:
$var = "echo \"FOZZ\";";
for($i = 0; $i < 100; $i++)
$var = "eval(\"".$var."\");";
print $var;
eval($var);
还有 eval 还有什么其他有趣的东西!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的陈述结果为 'eval("eval("echo"FOZZ";");")'。 您的双引号相互干扰并导致错误。
您可能想尝试将第一行更改为 $var = "echo \'FOZZ\';";
注意:请谨慎使用 eval。
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.
对于 Web 编程的常见情况,我开发了一个名为 xilla_tags 的库,可以轻松编写 lisp 风格的 PHP 代码,这比传统方法更具可读性。
对于 xilla_tags,请参阅 xilla 标签示例。
关于 PHP 中的 lisp 风格,Jacob Hannsen 在他的 bitchware 网站上有一篇很棒的文章,介绍了更一般的情况。
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.
它不起作用,因为您需要转义带引号的字符串内的引号。 由于
$var
包含引号,因此您需要在每次迭代时转义引号,并将其填充到另一层 eval 中。但无论如何,我不容忍像这样过度使用
eval()
。 即使使用一次也应该很少,并且只在最必要的情况下才使用。 任何编程语言中的eval()
都是等待发生的意外 。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.