PHP “or” Syntax

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

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

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

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

    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:

    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.

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