php-如何将这个IF条件放在switch中?

时间:2011-07-30 05:57:02

标签: php switch-statement

请帮我纠正这段代码,我想把它放在内部开关:

switch ($urlcomecatid) {

 if ($urlcomeparentid == 1 || $urlcomeparentid == 2 || $urlcomeparentid == 3)
  break;

case "50":
case "51":
case "52":
case "109":
case "110":
    //do nothing and exit from switch
    break;
default:
    header ("Location:http://www.example.com/tech/tech.php"); exit();
    break;

}

2 个答案:

答案 0 :(得分:6)

正确的代码应该是

switch ($urlcomecatid) {
    case "50":
    case "51":
    case "52":
    case "109":
    case "110":
        //do nothing and exit from switch
        break;
    default:
        if ($urlcomeparentid == 1 || $urlcomeparentid == 2 || $urlcomeparentid == 3)
            break;
        header ("Location:http://www.example.com/tech/tech.php"); exit();
        break;
 }

答案 1 :(得分:0)

使用if代替switch

<?php
if(!in_array($urlcomeparentid, array(1, 2, 3) && !in_array($urlcomecatid, array('50', '51', '52', '109', '110')){
    header ("Location:http://www.example.com/tech/tech.php");
    exit();
}

你应该避免exit(),因为它会杀死你脚本的其余部分,这是你调试时潜在的问题来源。