1

我似乎无法弄清楚为什么这不能正常工作。这是正在设置的变量,但是在“类型”的最终结果中,它将其设置为 m 而不是 fm

cpanel_notifications - 1 远程服务器 -
1

if ($_POST['cpanel_notifications'] == 1){
$type = "m";
}
elseif($_POST['cpanel_notifications'] == 0){
$type = "nm";
}
elseif($_POST['cpanel_notifications'] == 1 && $_POST['remote_server'] == 1){
$type = "fm";
}
elseif($_POST['cpanel_notifications'] == 0 && $_POST['remote_server'] == 0){
$type = "fnm";
}

结果:米

4

5 回答 5

7

这是因为第一个if陈述是正确的。没有理由去任何一个elses

于 2012-12-21T18:49:16.357 回答
2

你需要做的是重新排序你的if

if($_POST['cpanel_notifications'] == 1 && $_POST['remote_server'] == 1){
    $type = "fm";
}
elseif($_POST['cpanel_notifications'] == 0 && $_POST['remote_server'] == 0){
    $type = "fnm";
}
elseif ($_POST['cpanel_notifications'] == 1){
    $type = "m";
}
elseif($_POST['cpanel_notifications'] == 0){
    $type = "nm";
}
于 2012-12-21T18:49:05.823 回答
1

只需更改条件顺序

if ($_POST['cpanel_notifications'] == 1){
    if ($_POST['remote_server'] == 1) { 
        $type = "fm";
    } else {
        $type = "m";
    }
}
elseif($_POST['cpanel_notifications'] == 0){
    if ($_POST['remote_server'] == 0) {
        $type = "fnm";
    } else {
        $type = "nm";
    }
}

甚至

if ($_POST['cpanel_notifications'] == 1){
    $type = ($_POST['remote_server'] == 1?"fm":"m");
}
elseif($_POST['cpanel_notifications'] == 0){
    $type = ($_POST['remote_server'] == 0?"fnm":"nm");
}
于 2012-12-21T18:51:03.423 回答
1

我同意指定将具有多个条件的语句向上移动的评论。作为一般的经验法则,您希望将最具体的语句放在最前面,并在查看条件列表时变得更通用。

于 2012-12-21T18:56:04.683 回答
-1

添加更多 =

if ($_POST['cpanel_notifications'] === 1){
$type = "m";
}
elseif($_POST['cpanel_notifications'] === 0){
$type = "nm";
}
elseif($_POST['cpanel_notifications'] === 1 && $_POST['remote_server'] === 1){
$type = "fm";
}
elseif($_POST['cpanel_notifications'] === 0 && $_POST['remote_server'] === 0){
$type = "fnm";
}

http://php.net/manual/en/language.operators.comparison.php

上面的链接向您展示了为什么添加额外的 = 符号。

于 2012-12-21T18:49:41.357 回答