February 28, 2014

PHP -- Shortening IF Using in_array

Say that you have a long IF statement like this one:

if('go' == $do OR 'with' == $do OR 'the' == $do OR 'wind' == $do OR 'blows' == $do){
    //do something
}
else{
    //do something else
}

Then you can  shorten that IF statement above using in_array like this:

if(in_array($do,array('go','with','the','wind','blows'))){
    //do something
}
else{
    //do something else
}

No comments:

Post a Comment