sizeof() PHP Array Elements Count

How to Count PHP Array Elements?

This PHP sizeof() function counts all elements in an array, or properties in an object. If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned

For objects, if you have SPL installed, you can hook into sizeof() by implementing interface Countable. The interface has exactly one method, sizeof(), which returns the return value for the sizeof() function

sizeof() function syntax
sizeof(var, mode)

sizeof() function parameters
1. var is required to specify an array
2. mode is optional and is set to COUNT_RECURSIVE or 1, sizeof() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. sizeof() does not detect infinite recursion

sizeof() function example
<?php
$fruits = array("a" => "Apple", "b" => "Orange", "c" => "Grapes", "d" => "Cherry");
$arrcount = sizeof($fruits);
echo $arrcount;
?>

The above sizeof() function output will be:
4


Related Topic count() - How to Count All Elements in PHP Array or Properties in an Object?
C Program to Implement String Hash Table Associative Array
PHP Code for XML Parsing
PHP Array for List of All Countries and USA States
CSV File to PHP Array Elements

nScraps.com 2011   Privacy Policy  Terms of Service  Feedback