How to find the number of elements in an array in PHP

Counting the elements in an array gives the length of the array. You can find the number of elements in an array using PHP function count($array).

In this topic I will show how you can use count() function on the array and display the number of elements in an array. Look below code how it is used:

Here, I have an array $languages and I am using count() function on the array. In this case output will be 5.

Take a look at the below code, I have written it in html and PHP, I want to display output in the browser.

I am displaying the array elements first and then displaying the count. By using count($languages) in line 20, I get the number of elements in the array. I created a folder named 'array' under xampp/htdocs. Then saved this file as index.php under xampp/htdoc/array.

If you run this in the browser using localhost/array, you will see below output:

count number of elements in an array in php

count number of elements in an array in phpConclusion

This is an example of using array function in PHP. While coding, specially, during string manipulation, you might have to use various PHP functions on string and array. Finding array length or number of elements in an array is very common while coding in PHP.