php eval() 返回
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直接来自 PHP 文档:
看起来您的字符串中某处存在语法错误。
Straight from the PHP documentation:
Looks like there is a syntax error somewhere in your string.
我认为这种使用
eval()
的方式可能适用于其他语言(我想到的是 JavaScript),但不适用于 PHP。发出命令“计算以下表达式:
a == a
”是有意义的,并且期望为 true 是正确的。但 PHP 的eval()
并不是这样工作的。这是一种将代码发送到解释器的简单、原始的方法。如果您 eval()ed$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'seval()
doesn't work that way. It is a simple, primitive method to send code to the interpreter. If you eval()ed$a
would be true afterwards.仍然不确定您对代码的意图,因为尚未得到回复。如果你想弄清楚一个变量是否等于一个同名的变量,你可以通过这样做来找出答案:
This returns true:
This returns false:
只是在黑暗中刺探为什么有人会写这个块代码在原帖中。
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:
This returns false:
Just a stab in the dark at why someone would write the block of code in the original post.