Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
How to find the Total Number of Elements in an Array in TypeScript ?
Next article icon

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

Last Updated : 09 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Next Article
How to find the Total Number of Elements in an Array in TypeScript ?
author
sravankumar_171fa07058
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-function
  • PHP-Questions

Similar Reads

  • How to find the Total Number of Elements in an Array in TypeScript ?
    In TypeScript, arrays are a common data structure used to store collections of elements. You can store multiple elements of different or the same data type inside them by explicitly typing. The below methods can be used to accomplish this task: Table of Content Using the length property Using the fo
    3 min read
  • How to get the number of lines in a file using PHP?
    Given a file reference, find the number of lines in this file using PHP. There are a total of 3 approaches to solve this. test.txt: This file is used for testing all the following PHP codes Geeks For Geeks Approach 1: Load the whole file into memory and then use the count() function to return the nu
    2 min read
  • How to find the index of an element in an array using PHP ?
    In this article, we will discuss how to find the index of an element in an array in PHP. Array indexing starts from 0 to n-1. Here we have some common approaches Table of Content Using array_search() FunctionUsing array_flip()Using a custom functionUsing a foreach LoopUsing array_keys FunctionUsing
    6 min read
  • Perl | Getting the Number of Elements of an Array
    An array in Perl is a variable used to store an ordered list of scalar values. An array variable is preceded by an "at" (@) sign. The size of an array can be determined using the scalar context on the array which returns the number of elements in the array Example 1: #!/usr/bin/perl # Initializing t
    2 min read
  • How to get elements in reverse order of an array in PHP ?
    An array is a collection of elements stored together. Every element in an array belong to a similar data type. The elements in the array are recognized by their index values. The elements can be subjected to a variety of operations, including reversal. There are various ways to reverse the elements
    4 min read
  • How to Insert a New Element in an Array in PHP ?
    In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key. We can insert an element or item in an array using the below functio
    5 min read
  • How to check an element is exists in array or not in PHP ?
    An array may contain elements belonging to different data types, integer, character, or logical type. The values can then be inspected in the array using various in-built methods : Approach 1 (Using in_array() method): The array() method can be used to declare an array. The in_array() method in PHP
    2 min read
  • How to Convert Number to Character Array in PHP ?
    Given a number, the task is to convert numbers to character arrays in PHP. It is a common operation when you need to manipulate or access individual digits of a number. This can be particularly useful in situations where you need to perform operations on the digits of a number, such as digital root
    3 min read
  • How to Find Union and Intersection of Two Unsorted Arrays in PHP?
    Given two unsorted arrays representing two sets (elements in every array are distinct), find the union and intersection of two arrays in PHP. Example: Input: arr1[] = {7, 1, 5, 2, 3, 6} , arr2[] = {3, 8, 6, 20, 7} Output: Union {1, 2, 3, 5, 6, 7, 8, 20} and Intersection as {3, 6, 7}These are the fol
    4 min read
  • How to get specific key value from array in PHP ?
    In this article, we will see how to get specific key values from the given array. PHP array is a collection of items that are stored under keys. There are two possible types of keys: strings and integers. For any type of key, there is a common syntax to get a specific value by key — square brackets.
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences