Home > Article > Backend Development > How to display the total number of arrays in php
In PHP, to display the total number of an array, you can use the count()
function. This article will introduce how to use this function to display the total number of arrays.
First, we need to create an array. You can create a simple array in the following way:
$my_array = array("apple", "orange", "banana", "pear");
This array contains 4 elements. Now we use the count()
function to get the total number of the array:
echo count($my_array);
The above code will output: 4
# The ##count() function will return the total number of elements of the array, and then we pass this value as a parameter to the
echo function to output the result to the screen.
sizeof() function to achieve the same effect. For example:
echo sizeof($my_array);The output result of the above code is also
4.
count() and
sizeof() functions cannot be used for non-array type objects, because these functions only accept array type parameters. If we use these two functions to handle non-array type objects, a warning message will appear.
sizeof() function because it is slightly faster than the
count() function.
count() or
sizeof() function. These two functions return an integer value, which can be easily used to determine whether the array is empty, and to obtain information such as the number of elements in the array.
The above is the detailed content of How to display the total number of arrays in php. For more information, please follow other related articles on the PHP Chinese website!