Home >Backend Development >PHP Tutorial >PHP function sizeof count to detect array length_PHP tutorial
php tutorial function sizeof count to detect array length
The function to detect the length of an array in PHP is sizeof count. Let’s look at a simple example*/
$colorlist = array("apple"=>"red", "grass"=>"green","sky"=>"blue","night"=>"black","wall"=> ;"white");
echo "The length of the array is: ".count($colorlist); //5
/*
count
The count() function counts the number of cells in an array or the number of attributes in an object.
For arrays, returns the number of elements, and for other values, returns 1. If the argument is a variable and the variable is not defined, 0 is returned. If mode is set to count_recursive (or 1), the number of array elements in a multidimensional array is recursively counted.
sizeof
Definition and Usage
The sizeof() function counts the number of cells in an array or the number of attributes in an object.
Grammar
sizeof(array,mode) parameter description
array required. Specifies the array or object to be counted.
mode is optional. Specifies the mode of the function. Possible values:
0 - Default. Multidimensional arrays (arrays within arrays) are not detected.
1 - Detect multidimensional arrays.
Note: This parameter was added in PHP 4.2.
Tip count sizeof
Note: When the variable is not set, or the variable contains an empty array, this function returns 0. You can use the isset() variable to test whether a variable is set.