How to get total number of elements used in array in PHP ?
Last Updated :
09 Oct, 2021
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
$array1 = array (
0 => 'php' ,
1 => 'java' ,
2 => 'css/html' ,
3 => 'python' ,
4 => 'c/c++'
);
echo count ( $array1 );
?>
|
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
$array1 = array (
0 => 'php' ,
1 => 'java' ,
2 => 'css/html' ,
3 => 'python' ,
4 => 'c/c++'
);
echo sizeof( $array1 );
?>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...