What is the use of count() function in PHP?

The PHP count() function is used to count total elements in the array, or something an object.

In PHP, the count() function is used to count the number of elements in an array or the properties of an object. It returns the number of elements in an array or the number of properties in an object.

Here’s an example of using count() with an array:

php
$array = [1, 2, 3, 4, 5];
echo count($array); // Output: 5

And here’s an example of using count() with an object:

php
$obj = new stdClass();
$obj->prop1 = 'value1';
$obj->prop2 = 'value2';
echo count((array)$obj); // Output: 2

In both cases, count() returns the number of elements in the array or the number of properties in the object. This function is particularly useful when you need to dynamically determine the size of an array or the number of properties in an object.