views:

186

answers:

6

Not a major problem but I was wondering if there is a cleaner way to do this. It would be good to avoid nesting my code with an unnecessary if statement. If $items is empty php throws an error.

$items = array('a','b','c');

if(!empty($items)) { // <-Remove this if statement
  foreach($items as $item) {
    print $item;
  }
}

I could probably just use the '@' error suppressor, but that would be a bit hacky.

+5  A: 

I wouldn't recommend suppressing the warning output. I would, however, recommend using is_array instead of !empty. If $items happens to be a nonzero scalar, then the foreach will still error out if you use !empty.

Zach Rattner
+1 suppressing warnings and errors is **never** a good idea.
Christian Sciberras
I hat the is_[array] function, this sound like a poor programming still. Let me explain why: Why asking that a variable is an array? You should know that is an array otherwise it mean that you are messing with the type of the variable. If your type is getting inconsistent you are looking for trouble. When you start using the is_* function it tend to be spread all over your code. And after all you never know if the is_* is necessary and your code is being unreadable. I suggest you to fix the origin of the type inconsistency instead.
mathk
+1  A: 
foreach((array)$items as $item) {}
Milan
+5  A: 
Christian Sciberras
Just what I wanted. If the variable is not an array the loop won't be run.Thanks.
Keyo
+6  A: 
$items = array('a','b','c');

if(is_array($items))) {
  foreach($items as $item) {
    print $item;
  }
}
Matt Williamson
This doesn't remove any lines, but the code is much more self documenting and easier to read.
Peter Ajtai
+1 this way if $items is array but is empty, the foreach will not run and there will be no error. but empty() doesn't guarantee if $items is an array, so an error is possible
kgb
+4  A: 

I think the best approach here is to plan your code so that $items is always an array. The easiest solution is to initialize it at the top of your code with $items=array(). This way it will represent empty array even if you don't assign any value to it.

All other solutions are quite dirty hacks to me.

FractalizeR
+1 for the "always an array"
mathk
A: 

i've got the following function in my "standard library"

/// Convert argument to an array.
function a($a = null) {
    if(is_null($a))
        return array();
    if(is_array($a))
        return $a;
    if(is_object($a))
        return (array) $a;
    return $_ = func_get_args();
}

Basically, this does nothing with arrays/objects and convert other types to arrays. This is extremely handy to use with foreach statements and array functions

  foreach(a($whatever) as $item)....

  $foo = array_map(a($array_or_string)....

  etc
stereofrog
sound like a big ugly hack.
mathk
@mathk, sounds like a wrong unfounded statement ))
stereofrog
because you better ask why in first place you got a null or object instead of an array. You are assuming that the type is inconsistent.And you spread the test all over your code.That is kind of defensive programming.
mathk
@mathk this is how dynamic languages work. There's no type checking at compile time and there's no other way to write a polymorphic function.
stereofrog
@stereofrog you must be kinding: I work with smalltalk, scheme, lisp never had to do that. Read the SICP book there is some good example on how to make polymorphic function
mathk
@mathk - sorry, i don't see how your personal programming experience justifies your above statement.
stereofrog
@stereofrog Read the SICP[1] book, how it does justify very simple: You said that `this is how dynamic languages work. [snip] there's no other way to write a polymorphic function`. I said this is wrong, because one could used dynamically type PL without doing type checking at run time. [1] http://mitpress.mit.edu/sicp/
mathk
@mathk ok thanks
stereofrog