User Tools

Site Tools


uphp:functions:sizeof

sizeof

WMPRO, WMMINI FW >= 1.0 WMMEGA FW >= 2.0

Return the number of elements in an array

Description

int sizeof ( array $array )

Parameter

$array: Any array object

Return Values

Integer number of elements in the array

Examples

<?
 
  $array1[0]=1;
  $array1[1]=3;
  $array1[2]=5;
  print(sizeof($array1)); // output 3
 
  $array2[0]=7;
  $array2[5]=9;
  $array2[10]=11;
  print(sizeof($array2)); // output 3
 
  $array3=array();        // blank array
  print(sizeof($array3)); // output 0
 
  $array3=indexed_array(1,100); // indexed array of bytes
  print(sizeof($array3));       // output 100
 
  $array4=array(1,2,3,4);                 // array of integers
  $array5=array("one",2,3.33333,$array4); // mixed multidimensional array
  print(sizeof($array5));                 // output 4
 
  $s="string";       // not an array
  print(sizeof($s)); // output 0
 
  print(sizeof($undefined)); // output 0
 
?>

Notes

Array indexes are 0-based, but the count of the number of elements in an array is 1-based. Furthermore, sizeof() may have no relationship at all to the index numbers, which can vary depending on how the array is defined (see the second example above where the 3rd element of $array2 is index number 10).

This is the uPHP version of the PHP function sizeof() which is an alias of the function count().

Multidimensional arrays are not counted recursively, i.e., each element which is an array adds '1' to the total count of elements (see above example).

As can be seen from the examples, uPHP always returns 0 for objects which are not arrays (mainline PHP will return 1 in some cases). If you are not certain the object is an array it is best to first use function is_array().

See Also

array() - Create an array, with optional values

array_key() - Return the key for an array index

array_keys() - Return keys for an array that has key/value pairs

explode() - Turn a string into an array

implode() - Turn an array into a string

indexed_array() - Create an array of a specific type and size

is_array() - Check if a variable is an array

json_encode() - JSON encode an array into a string, with optional method

print_r() - Dump the contents of an array to the current output

uPHP Variable Types and Limits

uphp/functions/sizeof.txt · Last modified: 2021/09/13 05:57 (external edit)