tags:

views:

358

answers:

7

This question on 'How to tell if a PHP array is empty' had me thinking of this question

Is there a reason that count should be used instead of empty when determining if an array is empty or not?

My personal thought would be if the 2 are equivalent for the case of empty arrays you should use empty because it gives a boolean answer to a boolean question. From the question linked above, it seems that count($var) == 0 is the popular method. To me, while technically correct, makes no sense. E.g. Q: $var, are you empty? A: 7. Hmmm...

Is there a reason I should use count == 0 instead or just a matter of personal taste?

As pointed out by others in comments for a now deleted answer, count will have performance impacts for large arrays because it will have to count all elements, whereas empty can stop as soon as it knows it isn't empty. So, if they give the same results in this case, but count is potentially inefficient, why would we ever use count($var) == 0?

A: 

Im sure this is write theory but code may not be right.

  <?php
  $Array[3];
  for($x = 0; x<3; X++)
  {
     if($Array[x == "")
     {
       //Do this
     }
  }
  ?>
H4cKL0rD
Put that code in a code tag (the 101010 button when editing a post)
Wallacoloo
A: 

There is no strong reason to prefer count($myArray) == 0 over empty($myArray). They have identical semantics. Some might find one more readable than the other. One might perform marginally better than the other but it's not likely to be a significant factor in the vast majority of php applications. For all practical purposes, the choice is a matter of taste.

Asaph
A: 

I remade my mind guys, thanks.

Ok, there is no difference between the usage of empty and count. Technically, count should be used for arrays, and empty could be used for arrays as well as strings. So in most cases, they are inter-changeable and if you see the php docs, you will see the suggestion list of count if you are at empty and vice versa.

Sarfraz
+5  A: 

I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead. If you simply need to know whether or not the array is empty then use empty.

prodigitalson
These functions indeed differ when the array is *not* empty.
Jacco
@Jacco: Im not disputing that. But if youre testing it its empty, i dont see what relevance that has - its a question with a boolean result which is what the function will return. In regards to what is considered empty in dont see how those criteria would produce the wrong answer unless the var your testing isnt an array inwhich case thats an entirely different issue.
prodigitalson
+4  A: 

I think it's only personal preference. Some people might say empty is faster (e.g. http://jamessocol.com/projects/count_vs_empty.php) while others might say count is better since it was originally made for arrays. empty is more general and can be applied to other types.

php.net gives the following warning for count though :

count() may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set.

In other words, if the variable is not set, you will get a notice from PHP saying it's undefined. Therefore, before using count, it would be preferable to check the variable with isset. This is not necessary with empty.

Laurent le Beau-Martin
A: 

Is there a reason that count should be used instead of empty when determining if an array is empty or not?

There is, when you need to do something on non-empty array knowing it's size:

if( 0 < ( $cnt = count($array) ) )
{
 echo "Your array size is: $cnt";
}
else
 echo "Too bad, your array is empty :(";

But I wouldn't recommend using count, unless you are 100% sure, that what you are counting is an array. Lately I have been debugging code, where on error function was returning FALSE instead of empty array, and what I discovered was:

var_dump(count(FALSE));

output:

int 1

So since then I am using empty or if(array() === $array) to be sure that I have array that is empty.

dev-null-dweller
+1  A: 

My personal preference is more for coding elegance (in relation to my specific use-case). I agree with Dan McG inasmuch that count() isn't responding with the correct datatype (in this case boolean) for the test in question forcing the developer to write more code to fill an 'if' statement.

Whether this has any significant impact on performance is only debatable for extremely large arrays (which you probably won't have enough memory allocation for anyway in most setups).

Particularly when it comes to PHP's $_POST array, it seems much more "logical" in my opinion to write/see:

if ( !empty ( $_POST ) ) {
    // deal with postdata
}
scrumpyjack