PHP “or” Syntax

前端 未结 8 2153
臣服心动
臣服心动 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: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.

提交回复
热议问题