Code has been added to clipboard!

PHP count()

Reading time 2 min
Published Aug 14, 2017
Updated Oct 10, 2019

PHP Array Length: Main Tips

  • This function is used to get the number of elements in an array (in other words, to get PHP array length).
  • Introduced in PHP 4, though mode parameter used for multidimensional arrays only appeared in PHP 4.2.

count() Function

To get you started, we'll view a simple example. In it, we have an array called $fruits. Using PHP count()function, we will be able to easily count the elements in contains, or define PHP array length:

Example
<?php
  $fruits = ['Banana', 'Cherry', 'Apple'];
  echo "Result: " . count($fruits);
?>
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

Learn Proper Syntax

If we wish our PHP count() function to work and let us get PHP array length easily, we should follow the syntax described below:

count(array, mode);

The parameters are explained in more detail in the table that follows:

Parameter Description
array Necessary. Specifies the array to count.
mode Optional. Specifies PHP array length counting mode:
0 - Default. Only counts elements in a particular array.
1 - Counts elements in all arrays a multidimensional array contains.

Counting Examples

The example we have here is meant to show you the difference between different PHP array size counting modes. In it, $fruits is a multidimensional array that contains three arrays that have seven elements between them. Using different modes fetches us different PHP array sizes:

Example
<?php
   $fruit = [
      'Banana' => ['Green', 'Yellow'],
      'Apple' => ['Green', 'Yellow', 'Red'],
      'Berry' => ['Blueberry', 'Strawberry']
   ]; 
   echo "Normal count: " . count($fruit)."<br>";
   echo "Multidimensional count: " . count($fruit, 1);
?>

Let's see a few more examples of getting PHP array length using count():

Example
<?php
   $fruit = [
      'Banana' => ['Green', 'Yellow'],
      'Apple' => ['Green', 'Yellow', 'Red'],
      'Berry' => ['Blueberry', 'Strawberry']
   ]; 
   echo "Normal count: " . count($fruit)."<br>";
   echo "Multidimensional count: " . count($fruit, 1);
?>
Example
<?php
   $fruits = ['Banana', 'Cherry', 'Peach'];
   $array_length = count($fruits);
   echo "Result: " . $array_length;
?>

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 27/01/2021
3176 People Used
Only 97 Left
Rating
5.0