如何缩短`if-statement` [英] How to shorten `if-statement`
本文介绍了如何缩短`if-statement`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的foreach循环中有一个if
语句.条件是大约125个字符长.还有其他方法可以缩短此时间吗?
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;
}
注意:很抱歉,条件值家伙'a'
,'b'
,...的意思是各种各样的字符串.
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');
然后使用内置函数in_array()中的php来检查$ col是否存在于数组中,在您的表中看起来像这样:
Then use php in built function in_array() to check whether $col exists in array, in your this looks like:
in_array($col, $array);
完整代码:
$array = array('a','b','c','d','e','f');
if(in_array($col, $array)) {
continue;
}
这篇关于如何缩短`if-statement`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文