Home >Backend Development >PHP Tutorial >PHP count
The PHP count() method is used to get the number of elements in an array. The PHP count() method is a built-in method in PHP. It performs the same task as the size() method does. Sometimes we need to know the number of an element present in an array or object, so for this purpose PHP provides the count() method. It may also return 0 value for an array or object that has no element in it or if it is an empty array and also for an array or object which is not set.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
count( array, mode );
Parameters:
Given below are the examples mentioned:
Example of PHP count() method for finding the number of elements in an array. Next, we write the PHP code to understand the method more clearly with the following example, where the count() method is used to find the number of the element present in the single-dimensional array, as below.
Code:
<?php // array of languages $Language = array( 'English', 'Math', 'Science', 'Hindi'); print( "The array is : " ); print( implode(', ', $Language )); print(".<br>"); // Calculating total number of elements present in an array. $result = count($Language); print( "The total number of elements present in an array are :" ); print( $result ); ?>
Output:
As in the above program the code “$result = count($Language);” find and return the number of elements is present in the array “Language” and which is displaying as well, as we can see in the output.
Example of PHP count() method for finding the number of counts for the multidimensional array.
Next, we write the HTML code to understand the PHP Count() method, where the length property is used to finding the number of counts of an element in the multidimensional array, as below.
Code:
<?php // multidimensional array of subjects $sub = array( 'Subjects' => array( 'English', 'Math', 'Science', 'Hindi', 'Social' ), 'facultyid' => array( 'fac12', 'fac36', 'fac78', 'fac60', 'fac03' ) ); print( "The count for multidimensional array. " ); print( " <br>" ); // Calculating total number of elements present in an multidimensional array. // recursive count print( "The count for multidimensional array with recursive. " ); print( count( $sub, 1 )); print( " <br>" ); print( "The count for multidimensional array with out recursive. " ); print( count( $sub )); print( " <br>" ); ?>
Output:
As in the above program, the “sub” array is created to store the subjects and the respective faculty id who is handling it. Later in the code, the count() method is used two ways to find the number of elements present in this multidimensional array. The first way is “count( $sub, 1 );”, which is a recursive way as indicated by passing the mode value to 1 and the second way is “count( $sub );” , which is not a recursive way. So the output count of the first way is 12 and the output count of the second way is 2. PHP count() method for finding the number of counts for the multidimensional array of different data types.
Next, we write the HTML code to understand the PHP Count() method, where the length property is used to find the number of counts of elements in the multidimensional array which is of different data types in different dimensions, as below.
Code:
<?php // multidimensional array of Marks $Marks = array( 'Subjects' => array( 'English', 'Math', 'Science', 'Hindi', 'Social' ), 'rollno' => array( 89, 78, 56, 45, 67 )); print( "The count for multidimensional array of different data types. " ); print( " <br>" ); // Calculating total number of elements present in an multidimensional array. // recursive count print( "The count for multidimensional array with recursive. " ); print( count( $Marks, 1 )); print( " <br>" ); print( "The count for multidimensional array with out recursive. " ); print( count( $Marks )); print( " <br>" ); ?>
Output:
As in the above program, the “Marks” array is created to store the subjects and the respective marks which is of different data types compare to the subject of a marks array. Later in the code, the count() method uses two ways to find the number of elements present in this multidimensional array, so the output count of the first way is 12 and the output count of the second way is 2.
The PHP count() method is a built-in method, which is used to get the number of elements present in an array.
The above is the detailed content of PHP count. For more information, please follow other related articles on the PHP Chinese website!