PHP array: count or sizeof?

后端 未结 8 901
慢半拍i
慢半拍i 2020-11-30 21:27

To find the number of elements in a PHP $array, which is faster/better/stronger?

count($array) or sizeof($array) ?

Ed

8条回答
  •  一整个雨季
    2020-11-30 22:03

    I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.

    I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)

提交回复
热议问题