Is it possible to translate a boolean needle into in_array?

Can I go false as a needle to in_array() ?

 if(in_array(false,$haystack_array)){ return '!'; }else{ return 'C'; } 

$haystack_array will only contain boolean values. The column is the result of several write requests. I am trying to find out if any of them returned, and therefore is not completed.

+4
source share
4 answers

PHP does not care about what you pass as your "needle", but you probably should also use the third (optional) parameter for in_array to make this a "rigorous" comparison. "false" in PHP will be checked as equal to 0 , '' , "" , null , etc.

+9
source

Yes, exactly the same as in your code example. Since in_array returns a boolean to indicate whether the search was successful (and not return a match), this will not cause any problems.

0
source

There has become a better way .

 <?php echo (in_array(false,array(true,true,true,false)) ? '!' : 'C'); echo (in_array(false,array(true,true,true,true)) ? '!' : 'C'); Output: !C 
0
source

Yes you can, but why don't you do it with one boolean variable:

 $anyFalseResults = false; ... your query loop { // do the query if( $queryResult == false ) $anyFalseResults = true; } 

at the end of the loop, $ anyFalseResults will contain what you are looking for.

0
source

All Articles