简体   繁体   English

PHP-和/或关键字

[英]PHP - and / or keywords

Is && the same as "and", and is || &&与“ and”相同,并且|| the same as "or" in PHP? 与PHP中的“或”相同?

I've done a few tests, and it seems they behave the same. 我已经进行了一些测试,看来它们的行为相同。 Are there any differences? 有什么区别吗?

If not, are there any other PHP signs that have word equivalents and do you think it makes the code easier to read? 如果不是,是否还有其他具有单词等效功能的PHP标志,您认为它使代码更易于阅读吗?

and and or have higher lower precedence than && and || andor具有更高的优先级低于&&|| . To be more exact && and || 更确切地说&&|| have higher precedence than assignment operator ( = ) while and and or have lower. andor优先级高于赋值运算符( = )。

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

Usually it doesn't make a difference, but there are cases when not knowing about this difference can cause some unexpected behaviour. 通常不会产生任何变化,但是在某些情况下,不知道该变化会导致某些意外行为。 See examples here: 在此处查看示例:

http://php.net/manual/en/language.operators.logical.php http://php.net/manual/zh/language.operators.logical.php

Yes, they are logically the same. 是的,它们在逻辑上是相同的。 (I believe "&&" and "||" are the preferred choice in the Zend coding standards , but I can't find any specific information on this, so it might all have been a dream. Or something.) (我相信“ &&”和“ ||”是Zend编码标准中的首选,但是我找不到关于此的任何特定信息,因此可能全都是梦。)

That said: 说:

  1. "&&" and "||" “ &&”和“ ||” are of a higher precedence than "AND" and "OR" (unlikely to ever be relevant, but you never know). 比“ AND”和“ OR”的优先级更高 (不太可能相关,但您永远不知道)。

  2. A lot of other languages use "&&" and "||", rather than the textual equivalents so it might be an idea to go with this. 许多其他语言都使用“ &&”和“ ||”而不是等效的文本,因此使用此语言可能是一个主意。

  3. As long as you use your choosen set of operators consistently it doesn't really matter. 只要您持续使用所选的一组运算符就没有关系。

What bothers me is: 让我困扰的是:

echo (false and false ? true : true);
// (empty/false)

You might guess there is only the possible output of "1" (true) as there is no case which could output a false... ...but it will be "" (false). 您可能会猜测只有可能的输出为“ 1”(真),因为没有任何情况下可能会输出“假” ... ...但是它将是“”(假)。

Using && as a operator in this case satifies at least my expectations: 在这种情况下,使用&&作为运算符至少可以满足我的期望:

echo (false && false ? true : true);
// 1

So, in some cases the usage matters significantly. 因此,在某些情况下,用法很重要。

The difference is on the precedence . 区别在于优先级 But not only compared with each other! 但不仅彼此相比!

In most cases you won't mind it, but there are specific cases when you have to take one step back and look at the big picture. 在大多数情况下,您不会介意的,但是在某些特定情况下,您必须退后一步来了解大局。 Take this, for example: 以这个为例:

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;

// The constant true is assigned to $h before the "and" operation occurs
// Acts like: (($h = true) and false)
$h = true and false;

var_dump($g, $h);

This will produce, respectively: 这将分别产生:

bool(false)
bool(true)

In other words, && has higher preference than = , which has higher precedence than and , as stated in http://php.net/manual/en/language.operators.precedence.php . 换句话说, &&具有比=更高的优先级, =优先级高于and ,如http://php.net/manual/en/language.operators.precedence.php中所述 (It is mentioned in other answers, but I think it's worth to detail, since a misuse can lead to logical errors) (在其他答案中也提到过,但我认为值得详细说明,因为滥用会导致逻辑错误)

I hope it may help you. 希望对您有帮助。 You can find more at http://php.net/manual/en/language.operators.logical.php 您可以在http://php.net/manual/en/language.operators.logical.php中找到更多信息

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

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