php – What's the best way to check that there are no elements in an array?


Which of the options is the most correct for checking a php array for the absence of elements in it?

  1. if ($arr) {...}
  2. if (empty($arr)) {...}
  3. if (isset($arr[0])) {...}
  4. if (count($arr)) {...}


Yes, all, in general, are correct. Only in the first and fourth cases there will be a type conversion to bool, and the third is suitable only for indexed arrays. There is also a sizeof($arr) option.

Scroll to Top