Skip to content
Related Articles
Open in App
Not now

Related Articles

How to get total number of elements used in array in PHP ?

Improve Article
Save Article
  • Last Updated : 09 Oct, 2021
Improve Article
Save Article

In this article, we will discuss how to get total number of elements in PHP from an array.  We can get total number of elements in an array by using count() and sizeof() functions.

Using count() Function: The count() function is used to get the total number of elements in an array.

Syntax:

count(array)

Parameters: It accepts a single parameter i.e. array which is the input array.

Return Value: It returns the number of elements in an array.

Program 1: PHP program to count elements in an array using count() function.

PHP




<?php
  
// Create an associative array 
// with 5 subjects
$array1 = array(
      0 => 'php',  
      1 => 'java',  
      2 => 'css/html'
      3 => 'python',
      4 => 'c/c++'
);
  
// Get total elements in array
echo count( $array1);
  
?>

Output

5

sizeof() Function: The sizeof() function is used to count the number of elements present in an array or any other countable object.

Syntax:

sizeof(array)

Parameters: It accepts only one parameter array which is the input array.

Return Value: It returns the number of elements present in an array.

Examples:

Input: array(10,20,30,50)
Output: 4

Input: array("Hello","geeks")
Output: 2

Program 2: PHP program to count number of elements using sizeof() function.

PHP




<?php
  
// Create an associative array
// with 5 subjects
$array1 = array(
      0 => 'php',  
    1 =>'java',  
      2 => 'css/html'
      3 => 'python',
      4 => 'c/c++'
);
  
// Get total elements in array
echo sizeof( $array1);
  
?>

Output

5

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!