How to understand OR operator in a PHP statement [closed]
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.
精彩评论