Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check if PHP Array is empty [closed]

This is mostly superficial but what's the best (faster performing, not personal preference or readability) way to check if an array is empty:

  1. count($arr) == 0
  2. empty($arr)
  3. $arr === array()
  4. Other?

My Guess is that

  1. Iterates as far as possible then returns
  2. Simply performs 1 after checking if the variable is an array
  3. Seems like it should be slow as it has to construct a new object to compare with

But that doesn't account for any compile time optimizations that it performs here.

Disclaimer

I'm not about to go through my code base changes all instances to the fastest possible method so please don't remind me it's micro optimzation. This is simple curiosity.

like image 835
Paystey Avatar asked Mar 04 '13 13:03

Paystey


1 Answers

    if(empty($arr))
    echo "Empty";
    else
    echo "Ok..!";

This is the fastest and secure way to check an array empty or not

like image 57
ajay Avatar answered Sep 22 '22 07:09

ajay