Le 06/02/13 23:48, Tyler Romeo wrote: <snip>
The only mistake it can lead to is if there's a typo in a variable name, but in cases like this:
There are a few more possible such as 0 or "0" being considered empty. And as you said, that hide the fact a variable is not defined, that is sometime useful, lot of the time hiding an error.
If I wanted to check an array is empty I would probably:
count( $array ) === 0
count() requires determining the size of the array, which is significantly slower than any of the solutions.
Your assumption is probably based on the meaning of the word "count" but does not reflect the PHP implementation for an array. That is actually a hashtable which keep track of the number of elements it contains, counting is just about returning that value.
In ext/standard/array.c:
// The count() function definition: PHP_FUNCTION(count) ... case IS_ARRAY: RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC));
// Counting the element in an array: static int php_count_recursive(zval *array, long mode TSRMLS_DC) .. cnt = zend_hash_num_elements(Z_ARRVAL_P(array));
That zend_hash_num_elements is in Zend/zend_hash.c so small I am pasting it fully:
ZEND_API int zend_hash_num_elements(const HashTable *ht) { IS_CONSISTENT(ht); return ht->nNumOfElements; }
As we can see, the array is ultimately a HashTable which has a property to give out the number of elements in it. It is incremented whenever one add an object to the HashTable :-]