如何一次获得多个条件的结果

时间:2019-12-07 21:16:12

标签: php conditional-statements

我什至不知道怎么问我的问题!

我有一个像这样的变量:

$value['state 1'] = true;
$value['state 2'] = false;
$value['state 3'] = false;
$value['state 4'] = true;

我想通过条件或其他方式保留每个具有false值的状态的字符串,如果所有状态都具有true值,则使用另一个字符串,例如:

$message = "";
if ($value['state 1'] !== true){
     $message .= "State one isn't working!\n";
} elseif ($value['state 2'] !== true){
     $message .= "State two isn't working!\n";
} elseif ($value['state 3'] !== true){
     $message .= "State three isn't working!\n";
} elseif ($value['state 4'] !== true){
     $message .= "State four isn't working!\n";
} else {
$message = "All states works!";
}

在此代码中,在第一个真实条件之后,不会检查其他条件,因此我以这种方式使用了switch case

switch (false){
     case $value['state 1']:
        $message .= "State one isn't working!\n";
     case $value['state 2']:
        $message .= "State two isn't working!\n";
     case $value['state 3']:
        $message .= "State three isn't working!\n";
     case $value['state 4']:
        $message .= "State four isn't working!\n";
        break;
     default:
        $message = "All states works!";
        break;
}

在此代码中,匹配第一种情况后,该情况后的所有字符串将添加到$ message变量中!

好吧,我想现在您知道我想做什么了!你能帮我吗?

3 个答案:

答案 0 :(得分:3)

将它们分开,最后检查您的$message变量:

$message = "";
if ($value['state 1'] !== true){
     $message .= "State one isn't working!\n";
}
if ($value['state 2'] !== true){
     $message .= "State two isn't working!\n";
}
if ($value['state 3'] !== true){
     $message .= "State three isn't working!\n";
}
if ($value['state 4'] !== true){
     $message .= "State four isn't working!\n";
}
if (!$message) {
$message = "All states works!";
}

答案 1 :(得分:2)

如果要检查所有数组索引,则可以使用类似的

if ($value['state 1'] !== true){
     $message .= "State one isn't working!\n";
} 

if ($value['state 2'] !== true){
     $message .= "State two isn't working!\n";
} 

if ($value['state 3'] !== true){
     $message .= "State three isn't working!\n";
} 

if ($value['state 4'] !== true){
     $message .= "State four isn't working!\n";
} 

if (!$message) {
    $message = "All states works!";
}

或者,如果您更改数组,则可以使用循环,而无需编写if (...)的100500

是循环实现:

$value = [
    'state_1' => [
        'is_work' => false,
        'error_text' => "State 1 isn't working!\n"
    ],
    'state_2' => [
        'is_work' => true,
        'error_text' => "State 2 isn't working!\n"
    ],
    'state_3' => [
        'is_work' => true,
        'error_text' => "State 3 isn't working!\n"
    ]
];

$message = "";

foreach ($value as $arr) {
    if ($arr['is_work'] !== true) {
        $message .= $arr['error_text'];
    }
}

if ("" === $message){
    $message = "All states works!";
}

echo $message;

输出: State 1 isn't working!

答案 2 :(得分:2)

您应该真正使用循环,以便将来轻松扩展条件:

<?php
$value['state 1'] = true;
$value['state 2'] = false;
$value['state 3'] = false;
$value['state 4'] = true;

$message = null;
foreach($value as $key => $state) {
    if($state !== true) {
        $message .= $key . " isn't working!\n";
    }
}
echo $message ?? 'all states working'; // null-coalescing (PHP 7+)

//If your php version is older you can echo the message using a regular ternary expression:
print $message ? $message : 'all states working';

demo