2012-12-21 97 views
1

我似乎无法弄清楚为什么这不能正常工作。下面是变量被设置,然而,在 “类型” 的最终结果将其设置到m,而不是调频如果,否则如果没有设置变量来设置正确的变量

cpanel_notifications - 1
remote_server的 - 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"; 
} 

结果:米

+1

的首要条件总是被判断为真 –

+0

第一个条件是真,那么的其他条件,其余的都没有检查 –

+1

@Alon正是.... else部分doenot任何意义,如果条件中,如果总是真 –

回答

2

你需要什么待办事项是重新排列,如果是

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"; 
} 
+0

必须将第二个elseif设置为1,以便remote_server工作。谢谢 – user1913843

7

这是因为,第一if说法是正确的。没有理由去任何elses

-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

上面的链接告诉你为什么加入额外的等号(=)。

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"); 
} 
1

我指定要与多个条件了更高的移动你的陈述意见达成一致。作为一般的经验法则,您希望将最具体的语句放在最前面,并在条件列表中使用更具通用性的语句。