How to count all elements in an array in PHP?

To count all the elements in an array, PHP offers count() and sizeof() functions. The count() and sizeof() both functions are used to count all elements in an array and return 0 for a variable that has been initialized with an empty array. These are the built-in functions of PHP. We can use either count() or sizeof() function to count the total number of elements present in an array.

For example

We will discuss the program implementation for all the array elements using both count() and sizeof() methods.

Example 1: Counting using count()

Output

Number of elements present in the array: 4

Example 2

Output

Number of elements present in the array: 8

Example 3: Counting using sizeof()

Output

Number of elements present in the array: 6

Example 4

Output

Number of elements present in the array: 7

Example 5: Example using 2D array

Output

8
8

Next TopicPHP Tutorial




Contact US

Email:[email protected]

Count All Array Elements
10/30