2

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
vietean
  • 2,975
  • 9
  • 40
  • 65

1 Answers1

5

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.

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59