Saturday, August 16, 2008

PHP array length

As the number of elements can change time to time in your code it is important to get the actual length / size of the array. You have more option to get this information. PHP has 2 built in functions and both returns with the number of element in the array.
The first one is the count() function which counts elements in an array. You can use it like this:

Code:

  1. $colorList = array("apple"=>"red",

  2. "grass"=>"green",

  3. "sky"=>"blue",

  4. "night"=>"black",

  5. "wall"=>"white");

  6.  

  7. echo "Array size with count: ".count($colorList);

  8.  

PHP F1



The second function is the sizeof(). However sizeof() is only an alias of the count() so you can use it similar to count() the get the array length.

No comments: