What does "or" in php mean?

Possible Duplicate:
PHP - and / or keywords

I saw several bits of PHP code using or in a way I was unfamiliar with. For example:

fopen($site,"r") or die("Unable to connect to $site");

Is this equal to this || ?

Why would you use this instead of a try catch block? What will cause the program run the or die() ?


It is for the most part, but...

The reason for the two different variations of "and" and "or" operators is that they operate at different precedences.

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


or is equal to || except that || has a higher presedense than or .

Reference:

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


or has an other precedence. The concrete statement is little trick with boolean operators. Like in a common if -test-expression the second part is only executed, if the first is evaluated to false . This means, if fopen() does not fail, die() is not touched at all.

However, try-catch only works with Exceptions, but fopen() doesnt throw any.

Today something like this is "not so good" style. Use exceptions instead of hard abortion

if (!($res = fopen($site, 'r'))) throw new Exception ("Reading of $site failed");
链接地址: http://www.djcxy.com/p/57632.html

上一篇: PHP if语句:使用“OR”或

下一篇: “或”在PHP中是什么意思?