Find the Length of an Array in PHP

Definition

The count() function counts the elements of an array, or the properties of an  object.

The count() function has only two parameters, one of which is optional.

Required: array – specifies the array or object to count.

Optional: mode – Specifies the mode of the function.

Possible values:

0 – Default. Does not detect multidimensional arrays (arrays within arrays)

1 – Detects multidimensional arrays

Tip: The (optional) mode parameter was added in PHP 4.2

This function may return 0 if a variable isn’t set, but it may also return 0 if a variable contains an empty array. The isset() function can be  used to test if a variable is set.

count(array,mode)

 

<?php
$movies = array(“300″, “Dracula”, “Rosewood”, “Flintstones”);
$result = count($movies);

echo $result;
?>

The output of the code above will be:

4

It’s really quite simple because:

1. 300

2. Dracula

3. Rosewood

4. Flintstones