Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array: count or sizeof?

Tags:

php

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

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

Edit

Thanks to Andy Lester, I have refined my question to mean from a multilingual perspective. The manual commenters say

"[sizeof] does not mean the same in many other languages based on C"

Is this true?

like image 313
Ben Avatar asked Oct 20 '10 02:10

Ben


People also ask

What is the difference between count () and sizeof () in PHP?

1. The sizeof() function is used to return the number of elements in an array. The count() returns the number of elements in the array.

How do I count the number of elements in an array PHP?

The count() function returns the number of elements in an array.

How do you count an array?

You can simply use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for a variable that isn't set.

What is the use of count function in PHP?

The count() function counts elements in an array, or properties in an object. It returns the number of elements in an array.


1 Answers

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)

like image 159
alex Avatar answered Oct 12 '22 00:10

alex