1// using count() we can get proper length of the array
2$names = array("Ankur","Raj","Ram","Suresh");
3// pass array into count() as parameter it will return array length
4echo count($names);
5
6// output : 4
1<?php
2 $vegetables = ["Cabbage", "Carrot", "Lettuce"];
3 $arrayLength = count($vegetables);
4 echo $arrayLength; //returns 3
5?>
1
2<?php
3$a[0] = 1;
4$a[1] = 3;
5$a[2] = 5;
6var_dump(count($a));
7
8$b[0] = 7;
9$b[5] = 9;
10$b[10] = 11;
11var_dump(count($b));
12
13var_dump(count(null));
14
15var_dump(count(false));
16?>
17 /* result
18
19
20int(3)
21int(3)
22
23Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12 // as of PHP 7.2
24int(0)
25
26Warning: count(): Parameter must be an array or an object that implements Countable in … on line 14 // as of PHP 7.2
27int(1)
28
29*/
30<?php
31$a=array("A","Cat","Dog","A","Dog");
32print_r(array_count_values($a));
33?>
34 <?php
35 $vegetables = ["Cabbage", "Carrot", "Lettuce"];
36 $arrayLength = count($vegetables);
37 echo $arrayLength; //returns 3
38?>
39$cars=array("Volvo","BMW","Toyota");
40echo count($cars);
41//returns 3
1
2<?php
3$a[0] = 1;
4$a[1] = 3;
5$a[2] = 5;
6var_dump(count($a));
7
8$b[0] = 7;
9$b[5] = 9;
10$b[10] = 11;
11var_dump(count($b));
12
13var_dump(count(null));
14
15var_dump(count(false));
16?>
17 /* result
18
19
20int(3)
21int(3)
22
23Warning: count(): Parameter must be an array or an object that implements Countable in … on line 12 // as of PHP 7.2
24int(0)
25
26Warning: count(): Parameter must be an array or an object that implements Countable in … on line 14 // as of PHP 7.2
27int(1)
28
29*/
30
31