tuxnani tuxnani - 1 year ago 59
PHP Question

Which is a better way to check if an array has more than one element?

I just need to check if an array has more than one element.
I am trying to do it this way :

if (isset($arr['1']))

the other traditional way is

if (sizeof($arr)>1)

Which of the two is better? In such situaions, how should I judge between two alternate methods?
Is there any performance check meter available to measure which is better?

Answer Source

Use this

if (sizeof($arr) > 1) {


if (count($arr) > 1) {

sizeof() is an alias for count(), they work the same.

Edit: Answering the second part of the question: The two lines of codes in the question are not alternative methods, they perform different functions. The first checks if the value at $arr['1'] is set, while the second returns the number of elements in the array.