如何避免对需要执行的特定变量字符串使用 eval
我使用 eval()
进行类似的操作:
$result = eval("return ".$value1.$operator.$value2.";");
其中运算符来自变量或数据库字段。
在不使用 eval
的情况下,我如何才能获得相同的结果?是否可以?
这不是安全问题,因为值/运算符不是用户输入的,但如果 这条注释 是可以参考的。另外,如果在某个时候我想尝试 Facebook 的 HipHop,我需要用其他东西替换 eval
的所有使用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@Gumbo 提到的,您还可以使用
switch()
As @Gumbo mentioned, you can also use
switch()
好吧,我不确定运算符,但您可以使用函数名称执行类似的操作:
您甚至可以使用内置函数执行此操作:
Well, I'm not sure about operators, but you can do something like this with function names:
You can even do this with builtin functions: