Check for empty array in PHP

  • benwade9721
  • Novice
  • Novice
  • User avatar
  • Joined: Jan 20, 2009
  • Posts: 17
  • Status: Offline

Post June 6th, 2009, 3:38 pm

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($array_name):
if(!empty($array_name)):
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):

Cheers,

Ben
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 6th, 2009, 3:38 pm

  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post June 6th, 2009, 7:09 pm

That is reliable... an empty array always returns false

http://us2.php.net/empty (Check the return values).
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 216 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.