Check if multiple values are all false or all true

sandy picture sandy · Jul 27, 2011 · Viewed 31.5k times · Source

How can I check if 20 variables are all true, or if 20 variables are all false?

if possible without using a really long if ...

the variables are actually array elements:

array('a'=> true, 'b'=> true ...)

to make it more clear:

  • if the array has both true and false values return nothing
  • if the array has only true values return true
  • if the array has only false values return false :)

Answer

barfoon picture barfoon · Jul 27, 2011
if(count(array_unique($your_array)) === 1)  
  return current($your_array);

else return;