PHP - 不是运算符,还有其他别名吗?

发布于 2024-10-16 05:55:32 字数 173 浏览 5 评论 0原文

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 技术交流群。

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

发布评论

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

评论(5

薯片软お妹 2024-10-23 05:55:32
function not($OriginalCheck)
{
    return !$OriginalCheck;
}

function is($OriginalCheck)
{
    return !!$OriginalCheck;
}

应该这样做:)

有几种方法可以编写检查:

  • 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...

所有这些方法都是直观的并且是整个编程世界所熟知的。

function not($OriginalCheck)
{
    return !$OriginalCheck;
}

function is($OriginalCheck)
{
    return !!$OriginalCheck;
}

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.

╄→承喏 2024-10-23 05:55:32

不,不能。请参阅 http://www.php.net/manual/en/language。 Operators.logic.php 有关逻辑运算符的语言参考,并导航以查找其他别名。

但请注意,&&|| 的优先级与 andor 不同。

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 as and and or.

稚然 2024-10-23 05:55:32

一种选择是使布尔表达式更加明确:

if(($whatever && what()) == false) // do_stuff...

或者,通过实现自定义 not()

function not($expr) {
    return $expr == false;
}

if(not($whatever && what())) // do_stuff...

One option is to make the boolean expression more explicit:

if(($whatever && what()) == false) // do_stuff...

Or alternatively, by implementing a custom not():

function not($expr) {
    return $expr == false;
}

if(not($whatever && what())) // do_stuff...
时光倒影 2024-10-23 05:55:32

实际上,我也不喜欢 ! 运算符。虽然每个开发人员都能理解,但每次阅读代码都会花费我一些时间。所以我宁愿不要使用 ! 运算符。但这也取决于。

对于 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

哑剧 2024-10-23 05:55:32

没有替代的 !,但它可以写得不太直观:

if ($whatever - 1) {
}

如果问题原因是 ! 太容易被忽视了,但更明显;那么另一种替代符号是:

if (!!! $whatever) {

如果这看起来仍然很简单,只需使用:

if (~$whatever & 1) {

二元运算总是看起来很专业;)

There is no alternative !, but it could be written less intuitive:

if ($whatever - 1) {
}

Should the question cause be that ! is too easy to overlook, but not more visible; then another alternative notation would be:

if (!!! $whatever) {

If this still looks to simple, just use:

if (~$whatever & 1) {

Binary operations always look professional ;)

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