PHP 条件运算符和自赋值
这种事情在 PHP 中被认为是可以的吗?
$foo = $_GET['foo'];
$foo = empty($foo) || !custom_is_valid($foo) ? 'default' : $foo;
有更清洁的替代品吗? 我基本上试图避免额外的表查找。
Is this sort of thing considered OK in PHP?
$foo = $_GET['foo'];
$foo = empty($foo) || !custom_is_valid($foo) ? 'default' : $foo;
Are there cleaner alternatives to this? I'm basically trying to avoid extra table look-ups.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
怎么样:
不要害怕数组查找,它们并没有那么慢:)
How about:
And don't be afraid of the array lookups, they are not that slow :)
也许不只是检查它是否有效,而是通过采用默认值的清理函数来运行它。
另外,我喜欢使用以下函数,这样在运行 E_STRICT 时就不会收到有关访问不存在的数组键的警告:
Perhaps instead of just checking if it is valid, run it though a cleaning function that takes a default.
Also, I like to use the following function so I don't get warnings on accessing non-existant array keys when running E_STRICT:
在这里上课会让你的生活变得更加轻松。
A class here would make your life a lot easier.
custom_is_valid() 是否检查空变量? 因为能够删除empty()和“or not”将对改进该代码大有帮助。
Does custom_is_valid() check for an empty variable? Because being able to remove the empty() and "or not" would go a long way to improving that code.
正如您将看到的,如果您打开
error_reporting(E_ALL)
,这实际上并不是最好的方法。 PHP 基本上希望你做As you'll see if you turn
error_reporting(E_ALL)
on, that isn't really the best way to do it. PHP basically wants you to do