How to get number of element 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!

We can get total number of elements in an array by using count() and sizeof() functions.

Count() Function

The count() function is used to get the total number of elements in an array and it returns numbers of element in an array.

Syntax= count(array)

example

<?php
      $array1 = array(  'php','java','css', 'python','c++');
    $array2 = array
     (
      0 => 'php',  
      1 => 'java',  
      2 => 'css', 

      3 => 'python',
      4 => 'c++'
     );
  

   echo count( $array1)."<br />";
     echo count( $array2);

  
?>

output

5
5

Sizeof() Function

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

syntax

sizeof(array)

example

<?php
      
    $array2 = array
     (
      0 => 'php',  
      1 => 'java',  
      2 => 'css', 

      3 => 'python',
      4 => 'c++'
     );
  

     echo count( $array2);

  
?>

output

5
  • Both are used to count elements in a array sizeof() function is an alias of count() function used in PHP. count() function is faster and butter than sizeof().

Related Posts

Modernizing Software Delivery in the Dutch Tech Landscape: A Pragmatic DevOps Playbook

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

Finding Qualified AWS DevOps Trainers: Enterprise Playbook

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

Teaching Failure: How to Build Incident Response and Resilience into DevOps Training

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

The Open Road in Japan: An Independent Rider’s Handbook for Cross-Country Touring

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

A Comprehensive Guide to Scooters, Motorcycles, and Road Trips

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

Monitoring and Observability Training: Complete Curriculum Guide

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
0
Would love your thoughts, please comment.x
()
x