samedi 25 avril 2015

(Why) should I test if an array is empty prior to a for loop?

Given an empty array $items = array();

Why should I use the following code (which I've seen used before):

if (count($items) > 0) {
   foreach ($items as $item) // do stuff here
}

instead of just

foreach ($items as $item) // do stuff here

If count($items) === 0 the loop won't execute anyway??

Aucun commentaire:

Enregistrer un commentaire