Home  >  Article  >  Backend Development  >  How to calculate array length in php?

How to calculate array length in php?

巴扎黑
巴扎黑Original
2017-08-04 16:55:1717428browse

How to calculate array length in php?

#How does php calculate the length of an array?

In PHP, you can use the count() or sizeof function to get the total length or total number of elements present in the array and calculate the array length.

Method 1: count() function

First we create an array, as follows:

$array = array  ("ABC","DEF", "GHI","KLM");

Then we use the count function to print out this The number of elements in the array.

print count($array);

Output result:

How to calculate array length in php?

Output 4 as shown in the figure, because there are four elements in the above array.

Note: The count() function counts the number of cells in the array or the number of attributes in the object.

Method 2: sizeof() function

We can also use the sizeof function to determine the number of values.

Code example:

$value= array(2,5,6,8,9);
echo "数组值的数量是".sizeof($value)."<br>";

The output result is as follows:

How to calculate array length in php?

The above calculation output is 5.

Note: The sizeof() function calculates the number of cells in the array or the number of attributes in the object. The sizeof() function is an alias of the count() function.

For more related knowledge, please visit PHP Chinese website! !

The above is the detailed content of How to calculate array length in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn