PHP “or” Syntax

前端 未结 8 2112
臣服心动
臣服心动 2020-12-14 06:29

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

相关标签:
8条回答
  • 2020-12-14 07:05

    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()).

    0 讨论(0)
  • 2020-12-14 07:05

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

    <?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:

    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.

    Reference: [enter link description here][1]

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

    0 讨论(0)
  • 2020-12-14 07:05

    It can be used as || but hasn't the same precedence: 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.

    0 讨论(0)
  • 2020-12-14 07:09

    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.

    0 讨论(0)
  • 2020-12-14 07:13

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

    0 讨论(0)
  • 2020-12-14 07:15

    or just does a boolean comparison.

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

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

    0 讨论(0)
提交回复
热议问题