I have an array with booleans in it, whats the shortest way to loop through it and get the final boolean expression?
Example: array with 3 elems (true, false, true) --> the result should be false (true && false && true = false)
I know I can loop through it, but is there a shorter way?
Thanks!
Syntax To specify a boolliteral, use the constants trueor false. Both are case-insensitive. <?php $foo = True; // assign the value TRUE to $foo
In PHP, an empty array evaluates to false, while in JavaScript an empty array evaluates to true. In PHP, you can test an empty array as <?php if (!$stuff) …; ?> which won’t work in JavaScript where you need to test the array length.
But there may be times when it might be helpful to see the value of the Boolean as a 1 or 0. Here's how to do it. <?php $var1 = TRUE; $var2 = FALSE; echo $var1; // Will display the number 1 echo $var2; //Will display nothing /* To get it to display the number 0 for
$a = !array('a'); // This will === false; $s = ! ""; // This will === true; $s = !"hello"; // This will === false; To cast as if using a (bool) you can NOT the NOT with "!!" (double '!'), then you are casting to the correct (bool). Example: <?php $a = !!array(); // This will === false; (as expected)
if (!in_array(false, $array)) { ...
I don't knw php but you can
search for false in array first
if search is found then result is false else result is true.
Not: It's just and algorithm. not a code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With