You Can Use If, Instead Of count()

Posted: June 28, 2010 in PHP5, PHP5.3

If your using arrays as a collection for a container of data, very few know that count isn’t always needed to know if an array has values or not. Here is a comparison:

$array = array();

// without count,
// shall output nothing.
if($array)
  var_dump($array);

// with count,
// shall output nothing
if(count($array) > 0)
  var_dump($array); 

Both work 😉

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s