Php – Check for empty PHP array

arraycoding-stylePHP

An empty array in PHP is considered falsey. That means the following will not print anything as the array is empty.

<?php
$myArray = array()

if ( $myArray ) {
  print "My Array is NOT empty";
}

What is considered better practice in this case when determining if there are elements in the array:

  1. To use if ( !empty( $myArray ) )
  2. To use if ( myArray )

Please note: this question is not about subjectivity. I don't care what my team mate thinks. I care about best practices in the industry.

Best Answer

If the intent of your condition is to check that the array contains 0 elements, using count($array) === 0 is the best and most readable solution.