How to get a number of elements in an Array? explain with example

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.


Get Started Now!

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);
  
?>

Related Posts

What will you learn from Laravel?

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…

Read More

Error: MySQL shut down unexpectedly, How to solve this error on xampp?

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…

Read More

What is the requirement for learning Laravel?

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…

Read More

How to repair the corrupted table ‘global_priv’ in the localhost PHPMyAdmin database?

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…

Read More

Whenever you’re updating your PHP form data and getting a warning: count(): error message on the above page? How can you fix it?

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…

Read More

If details values are not retrieving at your PHP form whenever you update/edit your form. How can you fix it?

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…

Read More
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x