PHP“或”语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9535255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:02:31  来源:igfitidea点击:

PHP "or" Syntax

phpsyntaxprogram-flowcontrol-structureor-operator

提问by Andrew

I've seen this a lot: $fp = fopen($filepath, "w") or die();But I can't seem to find any real documentation on this "or" syntax. It's obvious enough what it does, but can I use it anywhere? And must it be followed by die()? Are there any caveats to using orwhen you could use something like

我已经看到了很多:$fp = fopen($filepath, "w") or die();但我似乎找不到任何关于这个“或”语法的真正文档。它的作用很明显,但我可以在任何地方使用它吗?并且必须跟在后面die()吗?or当您可以使用类似的东西时,是否有任何使用注意事项

if (file_exists($filepath))
   $fp = fopen($filepath,"r");
else
   myErrMessage();

I know it seems like a silly question, but I can't find any hard and fast rules for this. Thanks.

我知道这似乎是一个愚蠢的问题,但我找不到任何硬性规定。谢谢。

采纳答案by Smamatti

It's a logical operator and can be used in any logical expression.

它是一个逻辑运算符,可用于任何逻辑表达式。

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

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

回答by tacone

Let's just say that:

我们只想说:

$result = first() || second();

will evaluate to:

将评估为:

if (first()) {
    $result = true;
} elseif (second()) {
    $result = true;
} else {
    $result = false;
} 

while:

尽管:

$result = first() or second();

will evaluate to:

将评估为:

if ($result = first()) {
    // nothing
} else {
    second();
}

In other words you may consider:

换句话说,您可以考虑:

$result = first() || second();

$result = (first() || second());

and:

和:

$result = first() or second();

to be:

成为:

($result = first()) || second();

It is just matter of precedence.

这只是优先事项。

回答by Your Common Sense

This is neat trick, inherited from some PHP predecessor, based on the fact that PHP quite smartly won't evaluate any expression following OR, if first one returned true:

这是一个巧妙的技巧,继承自一些 PHP 前辈,基于这样一个事实,即 PHP 非常聪明地不会评估 OR 之后的任何表达式,如果第一个返回 true:

function a($ret){
    echo "FOO";
    return $ret;
}
function b($ret){
    echo "BAR";
    return $ret;
}

$result = (a(true) OR b(true));

will print out only FOO, means b() weren't even executed.

只会打印出来FOO,意味着 b() 甚至没有被执行。

回答by Chris

orjust does a booleancomparison.

or只是做一个布尔比较。

What's returned by fopen()can be treated as such a boolean value, because it returns FALSEif it fails (and a different value if it does not).

返回的内容fopen()可以被视为这样的布尔值,因为FALSE如果失败则返回(如果失败则返回不同的值)。

If it fails, the statement is evaluated to the right, and so the function die()is called.

如果失败,则语句向右求值,从而die()调用该函数。

回答by SenorAmor

Basically it means "if the first command fails, then perform the second command." In your example, if PHP cannot open the file, it will terminate the script (die()).

基本上它的意思是“如果第一个命令失败,则执行第二个命令。” 在您的示例中,如果 PHP 无法打开文件,它将终止脚本 ( die())。

回答by Carlos Quintanilla

'Or' in PHP is like C-like syntax (||)

PHP 中的“或”就像 C 类语法 (||)

<?php 
if( ($a==1 || $a==2) && ($b==3 || $b==4) && ($c==5 || $c==6) ) { 
     //do that something here. 
} 
?>

The 'Or' you are talking about is just a trick as the following states:

您正在谈论的“或”只是一个技巧,如下所述:

Example:

例子:

$result = mysql_query('SELECT foo FROM bar', $db) or die('Query failed: ' . mysql_error($db));

The or die() trick is a very poor choice for several reasons:

or die() 技巧是一个非常糟糕的选择,原因如下:

  1. It's not a very nice way to present the user with an error message.
  2. You cannot catch the error in any way.
  3. You cannot log the error.
  4. You cannot control whether it should be output to the screen or not. It's okay to do that in a development environment, but certainly not in a production environment.

    5. It prevents you from doing any sort of cleanup. It just ends the script abruptly.

  1. 向用户显示错误消息并不是一种很好的方式。
  2. 您无法以任何方式捕获错误。
  3. 您无法记录错误。
  4. 您无法控制它是否应该输出到屏幕上。在开发环境中这样做是可以的,但在生产环境中肯定不行。

    5. 它会阻止您进行任何类型的清理。它只是突然结束了脚本。

Reference: [enter link description here][1]

参考:[在此处输入链接描述][1]

[1]: http://www.phpfreaks.com/blog/or-die-must-dieenter code here

[1]:http: //www.phpfreaks.com/blog/or-die-must-dieenter code here

回答by Exupery

It can be used just like you'd use || as a logical OR http://php.net/manual/en/language.operators.logical.php

它可以像使用 || 一样使用 作为逻辑 OR http://php.net/manual/en/language.operators.logical.php

回答by Michael Laffargue

It can be used as ||but hasn't the same precedence: http://www.php.net/manual/en/language.operators.precedence.php

它可以用作||但没有相同的优先级:http: //www.php.net/manual/en/language.operators.precedence.php

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18.

运算符的优先级指定它如何“紧密”地将两个表达式绑定在一起。例如,在表达式 1 + 5 * 3 中,答案是 16 而不是 18,因为乘法 ("*") 运算符的优先级高于加法 ("+") 运算符。如有必要,可以使用括号来强制优先级。例如:(1 + 5) * 3 的计算结果为 18。