The function of the PHP sizeof function is to count the number of elements in the array or the number of attributes in the object. For example, we store many names in a PHP one-dimensional array . It is easy to call any person's name , But how to count how many people are stored in this PHP array? At this time, you can use PHP's sizeof function to count. In fact, the PHP sizeof function is an alias of the count function (Alias of count()), so the effect is the same.
PHP sizeof function syntaxsizeof(array, $mode);
The first argument PHP sizeof function is a PHP array, it is necessary projects, sizeof function will count the array number of elements in the array. The second parameter, $mode, is a new feature added to PHP 4.2. This is a non-essential item. The default value of $mode is 0, which means that multi-dimensional arrays are not detected . If set to 1, it means that multi-dimensional arrays are to be detected .If you want to further study the usage of PHP array, please refer to: PHP Array() Array function usage .Practical example of PHP sizeof function<?php
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; //Web page encoding declaration
$myarray=array("A","B", "C","D","E");
echo'There are a total of'.sizeof($myarray).' array elements in the array $myarray';
?>
Example outputThere are a total of 5 array elements in the array $myarray
At the beginning of the example, a $myarray array is prepared , which contains a total of five English letters from A to E. The results of the sizeof function statistics are output through echo . You can see that there are a total of 5 array elements counted by the sizeof function. The second parameter of the sizeof function is not used here, because it is only processing a one-dimensional array , and there is no need to use the second parameter. The above is a simple application example of the PHP sizeof function. If you want to use the count function, please refer to " Use PHP "count count the number of array elements " detailed introduction.
Post a Comment
0 Comments