简体   繁体   English

PHP:if(!$ one == $ two)不能总是正常工作吗?

[英]PHP: if(!$one == $two) doesn't work always?

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. 当第一个无效时,我总是需要像第二个一样重新编码。

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 另请参阅: http : //www.php.net/manual/zh/language.operators.precedence.php

! is having higher precedence than == so you should use parenthesis as: precedence高于==因此您应该使用括号作为:

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

You need 你需要

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

This is because without the brackets, it is checking if $one is false and then checking if $two == $one. 这是因为没有括号,它将检查$ one是否为false,然后检查$ two == $ one。 The following is the only time that it will work without the brackets. 以下是唯一没有括号的情况。 Evaluating to if (true == true) as !$one = true. 将if(true == true)评估为!$ one = true。

$one = false;
$two = true;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM