Friday, June 28, 2013

Count() function in PHP

The purpose of this function is to return the total count of elements in an array as shown in the example below.

<?php
 $a = array(12,13,14);
 echo count($a);  // 3
?>

 

It does not matter if the array is associative or its indices are not ordered. Check another example ::

<?php
 $a = array(12,13,14);
 $a[24] = 12;
 $a['my_name'] = 'Chandan';
 $a[134] = 102;
 echo count($a);  // 6
?>


Let's take an Multi-dimensional array and apply count().

<?php
 $a = array(
      "names" => array("John", "Martin", "Teddy"),
      "rolls" => array(10, 23, 3)
      );
 echo count($a);  // 2
?>

 

The above program prints 2 as count of primary indices "names" and "rolls". Let's modify the count() function call to get the correct element count in the array.

<?php
 $a = array(
      "names" => array("John", "Martin", "Teddy"),
      "rolls" => array(10, 23, 3)
      );
 echo count($a, COUNT_RECURSIVE);  // 8
?>


The second parameter to count() function forces it to get through each index recursively and search for sub-elements. Each of the 8 elements in the above array can be accesses as shown below :

$a['names']     // Value :: Array
$a['rolls']     // Value :: Array
$a['names'][0]  // Value :: John
$a['names'][1]  // Value :: Martin
$a['names'][2]  // Value :: Teddy
$a['rolls'][0]  // Value :: 10
$a['rolls'][1]  // Value :: 23
$a['rolls'][2]  // Value :: 3


The constant COUNT_RECURSIVE can be replaced with "1". Let's use the count() function with simple/basic type variables.

<?php
 $a = NULL;
 $b = '';
 $c = 'Smith';
 $d = 100;
 echo count($a);  // 0
 echo count($b);  // 1
 echo count($c);  // 1
 echo count($d);  // 1
?>


For any uninitialized simple/basic type variable, count returns zero. Let's use count() with objects.

<?php
class Foo
{
    public $var1 = 100;
    public $var2 = 200;
    public function display()
    {
      echo "$var1, $var2";
    }
}


$foo  = new Foo();
echo count($foo, COUNT_RECURSIVE); // 1
?>


Same behaviour noticed when we use objects instead of basic types. However we can convert an object to an array and then use count() function to know number of properties it has. Check the example below ::

<?php
$b = new stdClass;
$b->name = "John";
$b->roll_no = 100;
$b->age = 35;
$b->grade= 'b';
echo count((array)$b); // 4
?>


Let's use Countable interface which is defined in SPL (Standard PHP Library). By using this interface, we can manipulate the value count($object) returns. Check out the example below ::

<?php
class foo implements Countable
{
  private static $count = 0;
 
  public function __construct()
  {
     // Increment the count
     self::$count++;
  }
  // Our own implementation of count()
  public function count()
  {
    return self::$count;
  }
 
}
$b = new foo(); // count = 1
$c = new foo(); // count = 2

echo count($c); // 2
echo count($b); // 2
?>


Notice that the constructor is incrementing the value of static variable $count every time we create a new object. Our implementation of count() function manipulates the way count() works with our objects and it returns the value of $count (Class Property).

No comments: