To get the length of an array in PHP, you can use the count
function or the sizeof
function.
In this example,
$numbers
with some values.count
function or sizeof
function to get the length of the array and store the returned value in $length
variable.$length
to standard output.<?php
$numbers = [1, 2, 3, 4, 5];
$length = count($numbers);
echo "Length of array is: $length";
?>
Length of array is: 5
In this example,
$colors
with some values.count
function or sizeof
function to get the length of the array and store the returned value in $length
variable.$length
to standard output.<?php
$colors = ['red', 'green', 'blue'];
$length = count($colors);
echo "Length of array is: $length";
?>
Length of array is: 3
In this tutorial, we learned How to Get Array Length in PHP language with well detailed examples.