What will the `count()` function return when applied to NULL?

Prepare for the Zend Certified PHP Engineer Exam with our comprehensive test, featuring flashcards and multiple choice questions. Each question comes with detailed hints and explanations. Ensure you're ready for your exam!

The count() function in PHP is designed to count all elements in an array or something that implements the Countable interface. When count() is applied to a variable that is NULL, it treats NULL as an empty array. Since an empty array contains no elements, the count() function returns 0 in this case.

This behavior is consistent with the idea that when there are no elements to count, the result should logically equate to zero. Therefore, invoking count() on NULL results in a count of 0, indicating that there are no elements present.

Other options do not accurately reflect the behavior of the count() function. It does not return null, as this would imply an undefined or absent calculation, nor does it return 1, which would erroneously suggest the existence of one element. Returning false is also incorrect because false is a boolean value and does not represent a count. Thus, the correct answer is that count() returns 0 when applied to NULL.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy