3

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.

4

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.

  • Actually, you'll never get an error, as count() is defined for all types, it simply returns 1 for anything other than an array. However it will return 0 for an unset variable (which would raise a Notice), or for null: 3v4l.org/7lEGR Whether this particular set of cases is more or less useful than other "empty"/"falsey" values depends on the situation. – IMSoP May 3 '17 at 13:33
  • 2
    Of course. I forgot the most important php rule. Never make assumptions about the behavior of built-in functions. – Thijs Riezebeek May 3 '17 at 14:18
  • Well, more straight-forwardly, "don't expect built-in functions to complain about types". Throwing an error for the wrong type is very rare, most functions just try to give a reasonable answer. In this case, the function simply asks "how many things are in this variable?" - if it's null, then there are 0 things; if it's a scalar or non-countable object, there is 1 thing; if it's an array or collection object implementing Countable, then it looks how many things are in the list / collection. Another clue that the name is count not array_count. – IMSoP May 3 '17 at 14:52
  • I understand the importance of readability and the hate for empty(), but it should be mentioned that counting (= iterating the whole array) is much slower and could even result in memory issues when working with huge arrays and/or being careless with the memory in general. So, always be aware of what you count! :) – Marcello Mönkemeyer Jul 9 '18 at 16:31
  • @MarcelloMönkemeyer, actually, the C struct representing the array in the PHP core keeps track of the length of the array, so count() on a native array will only cause that int value to be returned. This makes the check very inexpensive. – Thijs Riezebeek Jul 10 '18 at 17:16
0

I'd say it depends on the "typical" audience who would be reading the code.

  1. If they are likely to be beginners, less familiar with PHP, or coming from a more verbose language (like Java), then they longer, more explicit if ( !empty( $myArray ) ) would be more clear.

  2. If they are moderately experienced with PHP, C, JavaScript, etc. then the terser, more idiomatic if ( $myArray ) would be my preference.

0

loose type is one of problems of PHP, but also it gives good developers more flexibility.

If you already defined $myArray = array(); Then you can just use if ($myArray), because you already know it is an array, and also the name tells you it is an array. falsy case would happen only when $myArray is empty.

But, you don't need to specify $myArray because there is a count() function, check if it is empty Countable. It will throw a warning when you pass a string of anything not countable. (above PHP7.2)

No matter what you prefer, there is no point to do if (!empty($myArray)) because you are coding in PHP. You need to take advantage of falsy values, not against it.

New contributor
Huawei Chen is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.