PHP 和宏(lisp 风格)?

发布于 2024-07-10 18:12:25 字数 333 浏览 9 评论 0原文

你好,我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

捂风挽笑 2024-07-17 18:12:25

您的陈述结果为 '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.

天邊彩虹 2024-07-17 18:12:25

对于 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.

不再见 2024-07-17 18:12:25

它不起作用,因为您需要转义带引号的字符串内的引号。 由于 $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文