Check for empty array in PHP

  • benwade9721
  • Novice
  • Novice
  • User avatar
  • Posts: 17

Post 3+ Months Ago

Hi everyone,

I am just wondering if these 3 lines of code for checking for an empty array in PHP do the same thing:
Code: [ Select ]
if(count($array_name) > 0):
  1. if($array_name):
  2. if(!empty($array_name)):
  3. if(count($array_name) > 0):

A print_r() outputs this: Array ( )

The 3 checks seem to be working as expected, I would like to use "if($array_name)" since it's the shortest but am concerned if it's reliable/consistent, it seems to be...

Does anyone have any ideas or why I shouldn't use if($array_name):


  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8489
  • Loc: USA

Post 3+ Months Ago

That is reliable... an empty array always returns false (Check the return values).

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 36 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.