Home Tutorial Php Phparray PHP Array Length Sizeof

 
 

Share on Google+Share on Google+
PHP Array Length Sizeof
Posted on: August 29, 2009 at 12:00 AM
Advertisement
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.

Advertisement

Related Tags for PHP Array Length Sizeof:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 29, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com