Home>Article>Backend Development> How to query the length of an array in php

How to query the length of an array in php

藏色散人
藏色散人 Original
2020-08-10 10:27:05 3544browse

How to query the length of an array in php: 1. Use the php function "count()" to get the length of the array. The syntax is such as "count($arr);"; 2. Use the "sizeof()" function to get the length of the array. To calculate the length of an array, the syntax is "sizeof($arr);".

How to query the length of an array in php

Recommended: "PHP Video Tutorial"

The method to get the length of an array in php is very simple, php does it for us Two functions are provided to calculate the length of a one-dimensional array, such as count and sizeof, which can directly count the length of the array. Let's take a look at a few examples.

How to get the length of an array in php, use the php function count(), or sizeof()

For example:

$arr = Array('0','1','2','3','4'); echo count($arr); // 输出 5 $arr = array('A','B','C'); echo sizeof($arr); //输出3

sizeof() and count() have the same Purpose, both functions can return the number of array elements. You can get the number of elements in a regular scalar variable. If the array passed to this function is an empty array, or a variable that has not been set, the returned The number of array elements is 0;

The two functions have the same function. According to the manual, sizeof() is an alias of the function count().

The above is the detailed content of How to query the length of an array 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