I have an if
statement in my foreach loop. The condition is about 125 characters long. Are there other ways to shorten this?
if ($col == 'foo' || $col == 'bar' || $col == 'baz' || $col == 'fubar' || $col == 'spam' || $col == 'eggs') {
continue;
}
NOTE: sorry for the confusion on the condition values guys, 'a'
, 'b'
, ... were meant to be various strings.
Store all elements in single dimension array first, in your case this will be look like:
$array = array('a','b','c','d','e','f');
Then use php in built function in_array() to check whether $col exists in array, in your this looks like:
in_array($col, $array);
Entire code:
$array = array('a','b','c','d','e','f');
if(in_array($col, $array)) {
continue;
}
I would use an array:
if(in_array($col, ['a','b','c','d','e','f'])) {
continue;
}
But be aware than this is not really an optimisation, more of a readability enhancement. There's nothing wrong with your previous statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With