开发者

How to understand OR operator in a PHP statement [closed]

开发者 https://www.devze.com 2023-04-05 06:10 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question s开发者_如何学编程o that it can be reopened, visit the help center. Closed 10 years ago.

I have a below PHP statement:

   if( 
        (bool)is_check1($a) || 
        (bool)is_check2($a) || 
        (bool)is_check3($a)
    ){
        callFunctionA();
    }

I have debugged and got a news thing so strange that, even if is_check1 returns TRUE, PHP still invoke the functions: is_check2, and is_check3.

In my mind, I always think that, if is_check1 function returns TRUE, PHP SHOULD not invoke others.

But when I check it again, for example:

   if( 
        TRUE ||
        (bool)is_check2($a) || 
        (bool)is_check3($a)
    ){
        callFunctionA();
    }

The result is: is_check2 and is_check3 function do not invoke.

Please give me your advice to optimize in this case or am I missing something?


Attempting to reproduce with the following code:

function a() { echo 'a'; return true; }
function b() { echo 'b'; return true; }
function c() { echo 'c'; return true; }

if (a() || b() || c()) echo 'valid!';
if (true || b() || c()) echo 'valid!';
if ((bool)a() || (bool)b() || (bool)c()) echo 'valid!';

Prints: avalid!valid!avalid!

That means the problem is probably the return values of your functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号