Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!
We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!
Learn from Guru Rajesh Kumar and double your salary in just one year.
In this query, we will discuss how to get number of elements in 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 : PHP program to count elements in an array using count() function.
<?php
// Create an associative array
// with 5 subjects
$array1 = array(
0 => 'php',
1 => 'Laravel',
2 => 'Bootstrap',
3 => 'python',
4 => 'C++'
);
// Get total elements in array
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
<?php
// Create an associative array
// with 5 subjects
$array1 = array(
0 => 'php',
1 =>'Laravel',
2 => 'Bootstrap',
3 => 'python',
4 => 'C++'
);
// Get total elements in array
echo sizeof( $array1);
?>