Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between sizeof and count in php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what is the difference between sizeof and count in php, it has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

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

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of getting the length of an array in PHP is simple, and PHP provides us with two functions to calculate the length of the 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 for the count () function. The functions and methods of the two functions are exactly the same.

Syntax format:

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

The parameters are described as follows:

$array: the array or object to be counted

$mode: optional parameter, which can be omitted.

If you omit the $mode parameter, or set it to COUNT_NORMAL or the 0account count () function will not detect multidimensional arrays

If $mode is set to COUNT_RECURSIVE or the 1 count () function will recursively count the number of elements in the array, it is especially useful for counting the number of elements in a multi-dimensional array.

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

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

Output result:

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

Output result:

You can see that the results returned using count () and sizeof () are the same.

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

Thank you for reading this article carefully. I hope the article "what is the difference between sizeof and count in php" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report