Home >Backend Development >PHP Problem >What is the difference between php sizeof and count

What is the difference between php sizeof and count

青灯夜游
青灯夜游Original
2021-09-23 12:00:472702browse

php There is no difference between sizeof() and count(). The sizeof() function is an alias of the count() function, which means that the function and usage of the sizeof() function are exactly the same as the count() function, and both can be used to calculate the length of an array.

What is the difference between php sizeof and count

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer

The method to obtain the array length in PHP is very simple. PHP provides us with two functions to calculate the length of an array, namely the count() and sizeof() functions.

But in fact, there is no difference between the count() and sizeof() functions. The sizeof() function is an alias of the count() function. The functions and usage of these two functions are exactly the same.

Grammar format:

count($array,$mode);
sizeof($array,$mode);

Parameter description is as follows:

  • $array: is the array or object to be counted;
  • $mode: is Optional parameter, can be omitted.
    • If the $mode parameter is omitted, or set to COUNT_NORMAL or 0, the count() function will not detect multidimensional arrays;
    • If $mode is set to COUNT_RECURSIVE or 1, the count() function Will recursively count the number of elements in the array, especially useful for counting the number of elements in multi-dimensional arrays.

Tip: If $array is neither an array nor an object, return 1; if $array is equal to NULL, return 0.

Example 1: Use count() to count the number of array elements.

<?php
header("Content-type:text/html;charset=utf-8");
$arr = [&#39;PHP中文网&#39;,&#39;PHP教程&#39;,&#39;//m.sbmmt.com/&#39;,&#39;count()函数&#39;,&#39;sizeof()函数&#39;,&#39;数组长度&#39;];
echo &#39;$arr 的长度为:&#39;.count($arr).&#39;<br>&#39;;
$arr2 = [&#39;C语言中文网&#39;,&#39;PHP教程&#39;,[&#39;//m.sbmmt.com/&#39;,&#39;count()函数&#39;,&#39;sizeof()函数&#39;,&#39;数组长度&#39;]];
echo &#39;$arr2 的长度为:&#39;.count($arr2).&#39;<br>&#39;;
echo &#39;参数 $mode = 1 时,$arr2 的长度为:&#39;.count($arr2, 1).&#39;<br>&#39;;
$str = &#39;//m.sbmmt.com/&#39;;
echo &#39;$str 的长度为:&#39;.count($str).&#39;<br>&#39;;
?>

Output result:

What is the difference between php sizeof and count

Example 2: Use sizeof() to count the number of array elements

<?php
header("Content-type:text/html;charset=utf-8");
$arr = [&#39;PHP中文网&#39;,&#39;PHP教程&#39;,&#39;//m.sbmmt.com/&#39;,&#39;count()函数&#39;,&#39;sizeof()函数&#39;,&#39;数组长度&#39;];
echo &#39;$arr 的长度为:&#39;.sizeof($arr).&#39;<br>&#39;;
$arr2 = [&#39;C语言中文网&#39;,&#39;PHP教程&#39;,[&#39;//m.sbmmt.com/&#39;,&#39;count()函数&#39;,&#39;sizeof()函数&#39;,&#39;数组长度&#39;]];
echo &#39;$arr2 的长度为:&#39;.sizeof($arr2).&#39;<br>&#39;;
echo &#39;参数 $mode = 1 时,$arr2 的长度为:&#39;.count($arr2, 1).&#39;<br>&#39;;
$str = &#39;//m.sbmmt.com/&#39;;
echo &#39;$str 的长度为:&#39;.sizeof($str).&#39;<br>&#39;;
?>

Output result :

What is the difference between php sizeof and count

It can be seen that the results returned by using count() and sizeof() are the same.

Note: Line 7 of the code sets $mode to 1, and the count() and sizeof() functions will cycle through all elements in the two-dimensional array. At this time ['https://www .php.cn/','count() function','sizeof() function','array length'] will be counted once as a whole, and the elements in it will be counted once, so the final result is 7.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the difference between php sizeof and count. 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