switch语句大小写未执行

时间:2019-10-01 09:01:38

标签: php

这样,我有40条以上的案例陈述,某些条件甚至没有执行,我不知道为什么请回答谢谢

switch (true){
    case (isset($_POST['min_beds']) && !empty($_POST['price_val']) && isset($_POST['property_status']) && $_POST['property_type'] == 'All' && $_POST['my_project_location'] == 'All'): 
    break;
    case (isset($_POST['min_beds']) && !empty($_POST['price_val']) && isset($_POST['my_project_location']) && $_POST['property_type'] == 'All' && $_POST['property_status'] == 'All'): 
    break;
    case (isset($_POST['min_beds']) && !empty($_POST['price_val']) && isset($_POST['property_status']) && $_POST['property_type'] == 'All' && $_POST['my_project_location'] == 'All'):
    break;

    case ($_POST['property_status'] == 'All' && isset($_POST['property_type']) && isset($_POST['my_project_location'])  && isset($_POST['min_beds']) && isset($features) && !empty($_POST['price_val'])): 
    break;
    case (isset($features) && !empty($_POST['price_val']) && isset($_POST['my_project_location']) && $_POST['property_status'] == 'All' && $_POST['property_type'] == 'All' && $_POST['min_beds'] == 'All'): 
    break;
    case ($_POST['property_status'] == 'All' && isset($_POST['my_project_location'])  && isset($_POST['min_beds']) && isset($features) && !empty($_POST['price_val']) && $_POST['property_type'] == 'All'): 
    break;
  }

2 个答案:

答案 0 :(得分:2)

与一系列elseif相比,Switch更像是if的链。仅将第一个case语句评估为真实的(当非严格比较所打开的值时) ,并且不会检查其他case语句。在匹配的break语句之后到达第一个case后,开关将退出;

请参阅:https://www.php.net/manual/en/control-structures.switch.php

switch(true) {
    case true:
        echo("this will run");
    case false: 
        echo("this will also run because there is no 'break' yet");
        break;
    case true:
        echo("this will never run");
}

答案 1 :(得分:0)

您没有正确使用开关。如果您希望在满足这些条件中的任何一个时中断,请使用ifs。

if(isset($_POST['min_beds']) && !empty($_POST['price_val']) && isset($_POST['property_status']) && $_POST['property_type'] == 'All' && $_POST['my_project_location'] == 'All'){
    break;
}
if(isset($_POST['min_beds']) && !empty($_POST['price_val']) && isset($_POST['my_project_location']) && $_POST['property_type'] == 'All' && $_POST['property_status'] == 'All'){ 
    break;
}
if(isset($_POST['min_beds']) && !empty($_POST['price_val']) && isset($_POST['property_status']) && $_POST['property_type'] == 'All' && $_POST['my_project_location'] == 'All'){
    break;
}
if($_POST['property_status'] == 'All' && isset($_POST['property_type']) && isset($_POST['my_project_location'])  && isset($_POST['min_beds']) && isset($features) && !empty($_POST['price_val'])){
    break;
}
if(isset($features) && !empty($_POST['price_val']) && isset($_POST['my_project_location']) && $_POST['property_status'] == 'All' && $_POST['property_type'] == 'All' && $_POST['min_beds'] == 'All'){ 
    break;
}
if($_POST['property_status'] == 'All' && isset($_POST['my_project_location'])  && isset($_POST['min_beds']) && isset($features) && !empty($_POST['price_val']) && $_POST['property_type'] == 'All'){ 
    break;
  }