php count函數

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   div   sp   cti   log   

最近被問到一個函數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函數實現的源碼是:

 1 PHP_FUNCTION(count) 2 { 3         zval *array; 4         long mode = COUNT_NORMAL; //預設不遞迴尋找元素個數 5        //擷取兩個參數 6         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &array, &mode) == FAILURE) { 7                 return; 8         }     9  10         switch (Z_TYPE_P(array)) {11                 case IS_NULL:12                         RETURN_LONG(0);//為空直接返回013                         break;14                 case IS_ARRAY:         //計算數組元素個數15                         RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC));16                         break;17                 case IS_OBJECT: {18 #ifdef HAVE_SPL19                         zval *retval;20 #endif21                         /* first, we check if the handler is defined */22                         if (Z_OBJ_HT_P(array)->count_elements) {23                                 RETVAL_LONG(1);24                                 if (SUCCESS == Z_OBJ_HT(*array)->count_elements(array, &Z_LVAL_P(return_value) TSRMLS_CC)) {25                                         return;26                                 }27                         }28 #ifdef HAVE_SPL //如果安裝了 SPL29                         /* if not and the object implements Countable we call its count() method */30                         if (Z_OBJ_HT_P(array)->get_class_entry && instanceof_function(Z_OBJCE_P(array), spl_ce_Countable TSRMLS_CC)) {31                                 zend_call_method_with_0_params(&array, NULL, NULL, "count", &retval);32                                 if (retval) {33                                         convert_to_long_ex(&retval);34                                         RETVAL_LONG(Z_LVAL_P(retval));35                                         zval_ptr_dtor(&retval);36                                 }37                                 return;38                         }39 #endif40                 }41                 default://其他類型返回142                         RETURN_LONG(1);43                         break;44         }45 }

以上可以看出,調用count函數時,當$mixed為數組時會調用php_count_recursive 函數

(php_count_recursive,當不是遞迴擷取元素個數時,返回數組的nNumOfElements,時間複雜度為N(1),只有mode=1時才會遞迴計算元素個數);

當$mixed是null時,返回0

預設都是返回1

    

結論:

    想瞭解一些PHP函數的時候還是多看看PHP手冊,以前主要是通過W3C上面瞭解的。

php count函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.