tags:

views:

57

answers:

3

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?

+1  A: 

According to the website, sizeof is an alias of count, so they should be running the same code. Perhaps sizeof has a little bit more lag because it needs to resolve it to count? It should be very minimal though.

Andy Groff
@Andy: Speculation like that serves no good. You're guessing, twice, about something that if there IS any difference is infinitesimal.
Andy Lester
Well, sorry for guessing? I was just stating that they're actually the same thing. Then, I made a logical inference that using an alias for a function instead of the actual function might be very minimally slower, but admited that i really have no idea by prefacing it with the word "perhaps." Sorry if that offended you. Maybe next time i'll use the word "infinitesimal."
Andy Groff
I'm not offended. I'm asking that you not guess if you, by your own admission, "don't know" and "have no idea".
Andy Lester
+1  A: 

They are identical according to http://php.net/manual/en/function.sizeof.php

In the absence of any reason to worry about "faster", always optimize for the human. Which makes more sense to the human reader?

Andy Lester
+6  A: 

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.

Update

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)

alex
Thanks exactly what I was looking for.
Steve