Home php教程 php手册 php count函数

php count函数

Jun 06, 2016 pm 07:40 PM
count php function

最近被问到一个 函数 count 1、count("123456") 对应的输出是什么? 2、count(null) 对应的输出是什么? 以前没有认真的考虑,只是心里有一个印象那就是count() 函数 是计算php数组的个数的。后来下来仔细看了看手册,原来发现: 1、count 函数 是统计数组

最近被问到一个函数count

1、count("123456") 对应的输出是什么?

2、count(null) 对应的输出是什么?

以前没有认真的考虑,只是心里有一个印象那就是count()函数是计算php数组的个数的。后来下来仔细看了看手册,原来发现:

1、count函数是统计数组里所有元素的个数或者对象的属性的个数的,函数原型如下:

    int count(mixed $var[, $mode=COUNT_NORMAL);

    对于对象,如果安装了SPL,可以通过Countable接口调用count,该接口只有一个Countable::count(),此函数返回count的返回值。

2、当设置$mode=1时,会递归对数组进行计算,但是识别不了无限递归。

3、当$var不是array或者object(实现了Countalbe::count函数)时:

    count函数将返回1,但当$mixed=null 时,将返回0

count函数实现的源码是:

<span> 1</span> <span>PHP_FUNCTION(count)
</span><span> 2</span> <span>{
</span><span> 3</span>         zval *<span>array;
</span><span> 4</span>         <span>long</span> mode = COUNT_NORMAL; <span>//</span><span>默认不递归查找元素个数
</span><span> 5</span>        <span>//</span><span>获取两个参数</span>
<span> 6</span>         <span>if</span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, <span>"</span><span>z|l</span><span>"</span>, &array, &mode) ==<span> FAILURE) {
</span><span> 7</span>                 <span>return</span><span>;
</span><span> 8</span> <span>        }    
</span><span> 9</span>  
<span>10</span>         <span>switch</span><span> (Z_TYPE_P(array)) {
</span><span>11</span>                 <span>case</span><span> IS_NULL:
</span><span>12</span>                         RETURN_LONG(<span>0</span>);<span>//为</span><span>空直接返回0</span>
<span>13</span>                         <span>break</span><span>;
</span><span>14</span>                 <span>case</span> IS_ARRAY:         <span>//</span><span>计算数组元素个数</span>
<span>15</span> <span>                        RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC));
</span><span>16</span>                         <span>break</span><span>;
</span><span>17</span>                 <span>case</span><span> IS_OBJECT: {
</span><span>18</span> <span>#ifdef HAVE_SPL
</span><span>19</span>                         zval *<span>retval;
</span><span>20</span> <span>#endif</span>
<span>21</span>                         <span>/*</span><span> first, we check if the handler is defined </span><span>*/</span>
<span>22</span>                         <span>if</span> (Z_OBJ_HT_P(array)-><span>count_elements) {
</span><span>23</span>                                 RETVAL_LONG(<span>1</span><span>);
</span><span>24</span>                                 <span>if</span> (SUCCESS == Z_OBJ_HT(*array)->count_elements(array, &<span>Z_LVAL_P(return_value) TSRMLS_CC)) {
</span><span>25</span>                                         <span>return</span><span>;
</span><span>26</span> <span>                                }
</span><span>27</span> <span>                        }
</span><span>28</span> #ifdef HAVE_SPL <span>//</span><span>如果安装了 SPL</span>
<span>29</span>                         <span>/*</span><span> if not and the object implements Countable we call its count() method </span><span>*/</span>
<span>30</span>                         <span>if</span> (Z_OBJ_HT_P(array)->get_class_entry &&<span> instanceof_function(Z_OBJCE_P(array), spl_ce_Countable TSRMLS_CC)) {
</span><span>31</span>                                 zend_call_method_with_0_params(&array, NULL, NULL, <span>"</span><span>count</span><span>"</span>, &<span>retval);
</span><span>32</span>                                 <span>if</span><span> (retval) {
</span><span>33</span>                                         convert_to_long_ex(&<span>retval);
</span><span>34</span> <span>                                        RETVAL_LONG(Z_LVAL_P(retval));
</span><span>35</span>                                         zval_ptr_dtor(&<span>retval);
</span><span>36</span> <span>                                }
</span><span>37</span>                                 <span>return</span><span>;
</span><span>38</span> <span>                        }
</span><span>39</span> <span>#endif</span>
<span>40</span> <span>                }
</span><span>41</span>                 <span>default</span>:<span>//</span><span>其他类型返回1</span>
<span>42</span>                         RETURN_LONG(<span>1</span><span>);
</span><span>43</span>                         <span>break</span><span>;
</span><span>44</span> <span>        }
</span><span>45</span> }
Copy after login

以上可以看出,调用count函数时,当$mixed为数组时会调用php_count_recursive 函数

(php_count_recursive,当不是递归获取元素个数时,返回数组的nNumOfElements,时间复杂度为N(1),只有mode=1时才会递归计算元素个数);

当$mixed是null时,返回0

默认都是返回1

    

结论:

    想了解一些PHP函数的时候还是多看看PHP手册,以前主要是通过W3C上面了解的。

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles