php eval() 返回

发布于 2024-09-25 10:19:31 字数 110 浏览 1 评论 0原文

<?php
  $a = "a == a";
  eval($a);

这会返回 false。我认为它应该返回 true。任何想法/想法为什么会这样。

<?php
  $a = "a == a";
  eval($a);

This returns false. I thought it's supposed to return true. Any thoughts/ideas why this is so.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小耗子 2024-10-02 10:19:31

直接来自 PHP 文档:

eval() 返回 NULL,除非在计算代码中调用 return,在这种情况下,将返回传递给 return 的值。如果计算的代码中存在解析错误,则 eval() 返回 FALSE,并且以下代码继续正常执行。

看起来您的字符串中某处存在语法错误。

Straight from the PHP documentation:

eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. If there is a parse error in the evaluated code, eval() returns FALSE and execution of the following code continues normally.

Looks like there is a syntax error somewhere in your string.

司马昭之心 2024-10-02 10:19:31

我认为这种使用 eval() 的方式可能适用于其他语言(我想到的是 JavaScript),但不适用于 PHP。

发出命令“计算以下表达式:a == a”是有意义的,并且期望为 true 是正确的。但 PHP 的 eval() 并不是这样工作的。这是一种将代码发送到解释器的简单、原始的方法。如果您 eval()ed

eval("$b = 5; $a = $b == $b;");

$a 之后将为 true。

I think this way of using eval() may work in other languages (JavaScript comes to mind), but it doesn't in PHP.

Issuing the command "Evaluate the following expression: a == a" makes sense and is right to expect true. But PHP's eval() doesn't work that way. It is a simple, primitive method to send code to the interpreter. If you eval()ed

eval("$b = 5; $a = $b == $b;");

$a would be true afterwards.

初心未许 2024-10-02 10:19:31

仍然不确定您对代码的意图,因为尚未得到回复。如果你想弄清楚一个变量是否等于一个同名的变量,你可以通过这样做来找出答案:

This returns true:

$a = $a == $a;
var_dump($a);

This returns false:

$b = 5;

$a = $a == $b;
var_dump($a);

只是在黑暗中刺探为什么有人会写这个块代码在原帖中。

Still not sure about your intentions for the code, since there has been no response. If you're wanting to figure out if a variable is equal to a variable by the same name you can find out by doing this:

This returns true:

$a = $a == $a;
var_dump($a);

This returns false:

$b = 5;

$a = $a == $b;
var_dump($a);

Just a stab in the dark at why someone would write the block of code in the original post.

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