PHP count() function

PHP count() function

The count() function in PHP is used to count all elements in the specified array or something in an object.

Syntax

count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )  

Parameter

array_or_countable(required)– This parameter represents the input array or countable object.

mode(optional)- This parameter is used to specify the mode. The possible mode values are as follows:

  • If it is set to integer value 0 (default value), it does not count all elements of multidimensional arrays
  • If set to integer value 1, it counts all the elements of multidimensional arrays recursively.

Return

This function returns the number of elements in the array.

It returns 1, if the parameter “array_or_countable” is neither an array nor an object.

It returns 0, if array_or_countable is NULL.

Example 1

<?php
 $fruits = array("Mango", "Banana", "Strawberry", "Apple");
 print_r($fruits);
 //will return the total number of the elements present in the array
 $count= count($fruits);
 echo("The count of the array is ".$count);
 ?> 

Output

Array
 (
     [0] => Mango
     [1] => Banana
     [2] => Strawberry
     [3] => Apple
 )
 The count of the array is 4 

Example 2

<?php
 $a[0] = 1;
 $a[1] = 3;
 $a[2] = 5;
 //counting the number of elements in an array
 var_dump(count($a));//will return int(3)
 ?> 

Output

int(3)

Example 3

<?php
 //initializing the array inside array
 $students=array (
   "Reema"=>array ( "Roll No"=>"15CS1029", "Coruse"=>"Btech CSE" )
   ,
   "Shivam"=>array( "Roll No"=>"15CS1019", "Coruse"=>"Btech Civil")
   , 
   "Monica"=>array( "Roll No"=>"15CS1022", "Coruse"=>"Btech Mechanical") 
  );
 //counting all the elements of multidimensional arrays
 echo "Recursive count: " . count($students,1);
 ?> 

Output

Recursive count: 9

Example 4

<?php
 $a[0] = 1;
 $a[1] = 3;
 $a[2] = 5;
 //counting the number of elements in an array
 var_dump(count(null));//will throw an error as the parameter is null
 ?> 

Output

PHP Warning:  count(): Parameter must be an array or an object that implements Countable in /workspace/Main.php on line 6

Example 5

<?php
 $a[0] = 1;
 $a[1] = 3;
 $a[2] = 5;
 //counting the number of elements in an array
 var_dump(count(false));//will throw an error as the parameter is false
 ?> 

Output

PHP Warning:  count(): Parameter must be an array or an object that implements Countable in /workspace/Main.php on line 6

Related Topics

PHP array() function

PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...

1 minute read.

PHP var_dump() Function

PHP var_dump() Function The var_dump() function in PHP is used to dump the information about a variable. It displays the structured information (arrays and objects) about one or more expressions. This function is also effective...

1 minute read.

PHP quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP krsort() Function

PHP krsort() Function The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys. Syntax krsort (array & $array[,int $sort_flags= SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This...

3 minutes read.

PHP echo() Function

PHP echo() Function The echo() function in PHP outputs or displays one or more strings. The major differences between print() function and echo() function is that echo accepts an argument list and doesn't have a return value. Also, it is slightly...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

1 minute read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP array_walk() Function

PHP array_walk() Function The array_walk() function in PHP is used to apply the user-defined callback function to each element of the array. This function returns a Boolean TRUE on success or FALSE on failure. Syntax array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] ) Parameter array(required)- This array represents...

1 minute read.

PHP Multidimensional Array

An array of arrays is called multidimensional array. It is used to store the data in a tabular form. It is represented in the form of matrix (row*column). Example: We can store...

2 minutes read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP chop() Function

PHP chop() Function The chop() function in PHP removes whitespaces or other predefined characters from the right end of the specified string. Syntax chop(str,charlist) Parameter str(required)- This parameter specifies the string to check. charlist(optional)- This parameter...

1 minute read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

1 minute read.

PHP String get_html_translation_table() Function

The get_html_translation_table() function in PHP is used to return the translation table used by the htmlentities() and htmlspecialchars() functions. Syntax get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]]] ) Parameter table (optional)- This parameter specifies which translation table to return. It...

2 minutes read.

PHP floatval() Function

PHP floatval() Function The floatval() function in PHP is used to give the float value of a variable. Syntax floatval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that is to be converted. Return This function...

1 minute read.

PHP reset() Function

PHP reset() Function The reset() function in PHP is used to set the internal pointer of an array to its first element and returns the value of the first array element. Syntax reset ( array &$array )  Parameter array(required)- This parameter represents...

1 minute read.