Jump to: navigation, search

Php count

From w3cyberlearnings

Contents

PHP function count

This function counts the element of an array.

Syntax count

  • array: array input
  • mode: 0 (default, not consider multidimensional array), 1 (treat array as multidimensional array)
count(array, mode);

Note

Alias of count() function is sizeof() function.

Example 1

<?php

$name = array('Taylor',
	 'Lucas',
	 'Makayla',
	 'Jennifer');

$size = count($name);

echo "There are {$size} people in the group.";
?>


Output

There are 4 people in the group.

Example 2


<?php
$team = array(
	 'group 1'=> array('Jennifer','Nevaeh','Elizabeth'),
	 'group 2'=> array('Grace','Lauren','Chase'),
	 'group 3'=> array('Kimberly','Jade')
);

$count = count($team,1);

echo $count;
?>



Output

11


Example 3

<?php

$team = array(
	 'group 1' => array('Jennifer', 'Nevaeh', 'Elizabeth'),
	 'group 2' => array('Grace', 'Lauren', 'Chase'),
	 'group 3' => array('Kimberly', 'Jade')
);

$count = count($team, 1); // get all elements

$group = count($team, 0); // get only the group

$players = $count - $group; // get all the players
echo "There are {$group} groups, and there are {$players} players.";
?>


Output

There are 3 groups, and there are 8 players.

Related Links


Navigation
Web
SQL
MISC
References