PHP比较多个字符串与1值

时间:2018-09-05 15:02:13

标签: php operators

我有一条比较简单的比较代码行,它可以工作,但是我想知道是否有一种更快/更有效的编写方式。

if($RES > 0 AND $MarkerL1 != 'RES' AND $MarkerL2 != 'RES' AND $MarkerL3 != 'RES' AND $MarkerL4 != 'RES' AND $MarkerL5 != 'RES' AND $MarkerL6 != 'RES' AND $MarkerL7 != 'RES' AND $MarkerL8 != 'RES'){$PosNumL9 = 8; $NextPosL9 = $RES; $MarkerL9 = 'RES';}

谢谢

3 个答案:

答案 0 :(得分:1)

您尝试以下代码:

$RES > 0  AND !in_array('RES',[$MarkerL1, $MarkerL2, $MarkerL3, $MarkerL4, $MarkerL5, $MarkerL6, $MarkerL7, $MarkerL8]) 

答案 1 :(得分:0)

您可能会这样:

    $markers = [$MarkerL1,$MarkerL2,$MarkerL3,$MarkerL4,$MarkerL5,$MarkerL6,$MarkerL7,$MarkerL8,$MarkerL9];

if($RES > 0 AND !in_array('RES',$markers))
{
$PosNumL9 = 8; $NextPosL9 = $RES; $MarkerL9 = 'RES';
}

希望对您有好处..

答案 2 :(得分:0)

一些已经强调的答案可以使用:

!in_array('RES',[$MarkerL1, $MarkerL2, $MarkerL3, $MarkerL4, $MarkerL5, $MarkerL6, $MarkerL7, $MarkerL8] 

作为

的快捷方式
$MarkerL1 != 'RES' AND $MarkerL2 != 'RES' AND $MarkerL3 != 'RES' AND $MarkerL4 != 'RES' AND $MarkerL5 != 'RES' AND $MarkerL6 != 'RES' AND $MarkerL7 != 'RES' AND $MarkerL8 != 'RES'

工作正常...

您还可以将完整代码更改为:

for($i=1,$false=true;$i<9;$i++){
    if(${"MarkerL$i"}==='RES'){
        $false=false;
        break;
    }
}
($RES>0 && $false)? $PosNumL9 = 8 && $NextPosL9 = $RES && $MarkerL9 = 'RES':null;

它也真的很好