PHP - 不是运算符,还有其他别名吗?
if(!($whatever && What()) do_stuff...
这可以替换为更直观的东西,例如:
if(not($whatever && What() )) do_stuff...
?
if(!($whatever && what()) do_stuff...
Can this be replaced with something more intuitive like:
if(not($whatever && what()) do_stuff...
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
应该这样做:)
有几种方法可以编写检查:
if(!($whatever && What()) do_stuff...
if(!$whatever || !什么()) do_stuff...
if(($whatever && What()) === false) do_stuff...
if((!$whatever) || !what()) === true) do_stuff...
if($whatever === false || What() === false) === true) do_stuff...
所有这些方法都是直观的并且是整个编程世界所熟知的。
should do exactly that :)
There are several ways to write checks:
if(!($whatever && what()) do_stuff...
if(!$whatever || !what()) do_stuff...
if(($whatever && what()) === false) do_stuff...
if((!$whatever || !what()) === true) do_stuff...
if($whatever === false || what() === false) === true) do_stuff...
all these ways are intuitive and known through out the programming world.
不,不能。请参阅 http://www.php.net/manual/en/language。 Operators.logic.php 有关逻辑运算符的语言参考,并导航以查找其他别名。
但请注意,
&&
和||
的优先级与and
和or
不同。No it can't. See http://www.php.net/manual/en/language.operators.logical.php for the language reference about logical operators, and navigate to find other aliases.
Note however that the precedence of
&&
and||
is not the same asand
andor
.一种选择是使布尔表达式更加明确:
或者,通过实现自定义
not()
:One option is to make the boolean expression more explicit:
Or alternatively, by implementing a custom
not()
:实际上,我也不喜欢
!
运算符。虽然每个开发人员都能理解,但每次阅读代码都会花费我一些时间。所以我宁愿不要使用!
运算符。但这也取决于。对于 php 内置函数,我几乎为 not 运算符创建了辅助函数。
https://github.com/vuvanly/not-operators-helpers
Actually, I don't like the
!
operator as well. Although it was understood by every developers but it took me a bit of time every time I read the code. So I prefer do not use the!
operator. But it depends on also.For php built-in function, I created almost the helper functions for not operator.
https://github.com/vuvanly/not-operators-helpers
没有替代的
!
,但它可以写得不太直观:如果问题原因是
!
太容易被忽视了,但不
更明显;那么另一种替代符号是:如果这看起来仍然很简单,只需使用:
二元运算总是看起来很专业;)
There is no alternative
!
, but it could be written less intuitive:Should the question cause be that
!
is too easy to overlook, butnot
more visible; then another alternative notation would be:If this still looks to simple, just use:
Binary operations always look professional ;)