php-如何将此 IF 条件放入开关内?

发布于 2024-11-27 08:06:59 字数 377 浏览 7 评论 0原文

请帮助我更正此代码,我想将 if 放在开关内:

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;

}

Please help me for correcting this code,i want put if inside switch:

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烏雲後面有陽光 2024-12-04 08:06:59

正确的代码应该是这样的

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;
 }

The correct code should be something like

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;
 }
零崎曲识 2024-12-04 08:06:59

使用 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() 因为它会杀死脚本的其余部分,这在调试时是潜在的问题来源。

Use an if instead of the 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();
}

You should avoid exit() as it kills the rest of your script which is a potential source of problems when you debug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文