gpt4 book ai didi

php - 如何缩短 `if-statement`

转载 作者:可可西里 更新时间:2023-11-01 00:05:11 25 4
gpt4 key购买 nike

我的 foreach 循环中有一个 if 语句。条件大约有 125 个字符长。还有其他方法可以缩短这个时间吗?

if ($col == 'foo' || $col == 'bar' || $col == 'baz' || $col == 'fubar' || $col == 'spam' || $col == 'eggs') {
continue;
}

注意:很抱歉对条件值的混淆伙计们,'a''b',...本来应该是各种字符串。

最佳答案

首先将所有元素存储在一维数组中,在您的情况下,它看起来像:

$array = array('a','b','c','d','e','f');

然后在内置函数 in_array() 中使用 php 检查 $col 是否存在于数组中,在你看来是这样的:

in_array($col, $array);

完整代码:

$array = array('a','b','c','d','e','f');
if(in_array($col, $array)) {
continue;
}

关于php - 如何缩短 `if-statement`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com