Maison  >  Article  >  php教程  >  php count函数

php count函数

WBOY
WBOYoriginal
2016-06-06 19:40:391170parcourir

最近被问到一个 函数 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> }

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

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

当$mixed是null时,返回0

默认都是返回1

    

结论:

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

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn