PHP: if(!$one == $two) 并不总是有效?

发布于 2024-09-19 16:34:57 字数 294 浏览 3 评论 0原文

是的,这只是我想得到答案的一个问题。我经历过几次,其中:

if(!$one == $two){ echo "Not the same"; }else{ echo "The same"; }

不会起作用,并且

if($one == $two){ echo "The same"; }else{ echo "Not the same"; }

会起作用。

为什么有时不起作用?当第一个不起作用时,我总是需要像第二个一样重新编码。

Yes, this is just a question i would like to get an answer on. I experienced it a couple of times, where this:

if(!$one == $two){ echo "Not the same"; }else{ echo "The same"; }

Will not work, and

if($one == $two){ echo "The same"; }else{ echo "Not the same"; }

will work.

Why doesn't it work sometimes? I always need to recode like the second, when the first doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

泛泛之交 2024-09-26 16:35:10

你需要

if(!($one == $two))

这是因为没有括号,它会检查 $one 是否为 false,然后检查 $two == $one 是否。以下是唯一一次不使用括号即可工作的情况。评估 if (true == true) as !$one = true。

$one = false;
$two = true;

if (!$one == $two)
{
    echo "different";
}

You need

if(!($one == $two))

This is because without the brackets, it is checking if $one is false and then checking if $two == $one. The following is the only time that it will work without the brackets. Evaluating to if (true == true) as !$one = true.

$one = false;
$two = true;

if (!$one == $two)
{
    echo "different";
}
疯到世界奔溃 2024-09-26 16:35:08

你需要写

if(!($one == $two))

if($one != $two)

因为 !运算符的优先级高于 == 运算符。

另请参阅: http://www.php.net/manual/en /language.operators.precedence.php

You need to write

if(!($one == $two))

or

if($one != $two)

since the ! operator has a higher precedence than the == operator.

See also: http://www.php.net/manual/en/language.operators.precedence.php

司马昭之心 2024-09-26 16:35:05

! 具有更高的优先级== 所以你应该使用括号:

if(!($one == $two))

! is having higher precedence than == so you should use parenthesis as:

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