Php function for count

PHP count() function with examples

PHP count() function is an in-built function available in PHP, which counts and returns the number of elements in an array. It also counts the number of properties in an object.

The count() function may return 0 for the variable, which has been declared with an empty array or for the variable which is not set.

PHP Version

count() function is compatible with PHP version 4 or above

Define the mode of the function; Possible values can be 0 or 1

0: Default value. Does not count all the elements of the multi-dimensional array

1: Count all the elements of the multi-dimensional

Return Value:

Number of elements in an array

Below is the simple example of the PHP count() function

Total Number of books is : 4

Advanced Examples

1) Using PHP count() function with for loop

//Program to print data using count and for loop '; > ?>
Book at index 0 is : PHP Programming Book at index 1 is : Programming Fundamentals Book at index 2 is : Modern PHP Book at index 3 is : PHP and MySQL

2) PHP count function with recursively or passing mode

//Program to print data using count() with recursion array( "Programming PHP", "Programming Fundamentals", "Modern PHP", "PHP and MySQL" ), 'bookAuthors' => array( "Rasmus Lerdorf", "S. K. Jha", "Josh Lockhart", "Joel Murach, Ray Harris" ) ); //Printing the total number of elements in the array without recursion echo "Total number of elements : ". count($productList,0); //Breaking the line echo "
"; //Printing the total number of elements in the array with recursion echo "Recursively count elements total is : ". count($productList,1); ?>

Output:

Total number of elements : 2 Recursively count elements total is : 10

Источник

count

Counts all elements in an array when used with an array. When used with an object that implements the Countable interface, it returns the return value of the method Countable::count() .

Parameters

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

count() can detect recursion to avoid an infinite loop, but will emit an E_WARNING every time it does (in case the array contains itself more than once) and return a count higher than may be expected.

Return Values

Returns the number of elements in value . Prior to PHP 8.0.0, if the parameter was neither an array nor an object that implements the Countable interface, 1 would be returned, unless value was null , in which case 0 would be returned.

Changelog

Version Description
8.0.0 count() will now throw TypeError on invalid countable types passed to the value parameter.
7.2.0 count() will now yield a warning on invalid countable types passed to the value parameter.

Examples

Example #1 count() example

$a [ 0 ] = 1 ;
$a [ 1 ] = 3 ;
$a [ 2 ] = 5 ;
var_dump ( count ( $a ));

$b [ 0 ] = 7 ;
$b [ 5 ] = 9 ;
$b [ 10 ] = 11 ;
var_dump ( count ( $b ));
?>

The above example will output:

Example #2 count() non Countable|array example (bad example — don’t do this)

$b [ 0 ] = 7 ;
$b [ 5 ] = 9 ;
$b [ 10 ] = 11 ;
var_dump ( count ( $b ));

The above example will output:

Output of the above example in PHP 7.2:

int(3) Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12 int(0) Warning: count(): Parameter must be an array or an object that implements Countable in … on line 14 int(1)

Output of the above example in PHP 8:

int(3) Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable .. on line 12

Example #3 Recursive count() example

$food = array( ‘fruits’ => array( ‘orange’ , ‘banana’ , ‘apple’ ),
‘veggie’ => array( ‘carrot’ , ‘collard’ , ‘pea’ ));

// recursive count
var_dump ( count ( $food , COUNT_RECURSIVE ));

// normal count
var_dump ( count ( $food ));

The above example will output:

Example #4 Countable object

class CountOfMethods implements Countable
private function someMethod ()
>

public function count (): int
return count ( get_class_methods ( $this ));
>
>

$obj = new CountOfMethods ();
var_dump ( count ( $obj ));
?>

The above example will output:

See Also

  • is_array() — Finds whether a variable is an array
  • isset() — Determine if a variable is declared and is different than null
  • empty() — Determine whether a variable is empty
  • strlen() — Get string length
  • is_countable() — Verify that the contents of a variable is a countable value
  • Arrays
Оцените статью