PHP Array Length Sizeof


 

PHP Array Length Sizeof

In this example and PHP program you will the use of PHP Array Length Sizeof function.

In this example and PHP program you will the use of PHP Array Length Sizeof function.

The PHP Array Length Sizeof example code.

In this series of PHP Tutorial, we are discussing about the length of array and how to get the actual length of array using "Sizeof" function in PHP. As you know PHP is a powerful scripting language that can be used to make more dynamic and interactive websites. And the use of array & array's functions makes it more simple to use, the best thing is that one can do lots of manipulation using array functions. In PHPCount() and sizeof() are the same type of functions, or in other words solves the same purpose but "sizeof()" is just a alias of count() function.

In the previous example you saw how to define an array, now we will show you how to get the size of array or number of elements in the array. See the below given syntax...

Syntax to get the length of array using "Sizeof" function

<?PHP
echo sizeof ($variable);
?>

PHP Array Length Sizeof example:

<?PHP

$colors = array( "Red", "Green", "Yellow", "Blue");

foreach ($colors as & $value) {
echo $value."<br>";
}

echo $colors;
//Retriving the size of array

echo sizeof($colors);
?>

Output of PHP Sizeof example

Red
Green
Yellow
Blue
Array4

As you can see in the output, the array size is 4 and which is correct as we have defined only four elements (Red, Green, Yellow, Blue) to the array. These type of functions are generally used when you really need to know about the size of array for example: In case we wanted to add or remove elements some elements from the list and when we actually do such things "sizeof" array function can help us a lot to find the actual size of array.

Ads