This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

  • PHP count() Function

  • Definition and Usage

    The count() function returns the number of elements in an array.

    Syntax

    count(array, mode)

    Parameter Values

    Parameter Description
    array Required. Specifies the array to sort
    mode Optional. Specifies the mode. Possible values:
    • 0 - Default. Does not count all elements of multidimensional arrays
    • 1 - Counts the array recursively (counts all the elements of multidimensional arrays)

    Technical Details

    Return Value: Returns the number of elements in the array
    PHP Version: 4+
    PHP Changelog: The mode parameter was added in PHP 4.2

    Examples

    Example 1: PHP count() Function part 1

    Return the number of elements in an array:

    <?php
    $cars=array("Volvo","BMW","Toyota");
    echo count($cars);
    ?>
    Example 2: count() Function part 2

    Count the array recursively:

    <?php
    $cars=array
      (
      "Volvo"=>array
      (
      "XC60",
      "XC90"
      ),
      "BMW"=>array
      (
      "X3",
      "X5"
      ),
      "Toyota"=>array
      (
      "Highlander"
      )
      );
    
    echo "Normal count: " . count($cars)."<br>";
    echo "Recursive count: " . count($cars,1);
    ?>
    Navigate this PHP reference guide

    Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.



    Animated PHP icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon

    PHP Quiz