Reputation: 135
I cant seem to figure out why this is not working correctly. Here are the variables being set, however in the end result of "type" it sets it to m and not fm
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";
}
Result: m
Upvotes: 1
Views: 125
Reputation: 13544
Add more =
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
The above link show you why adding the extra = sign.
Upvotes: -1
Reputation: 11
I agree with the comments specifying to move your statements with multiple conditions up higher. As a general rule of thumb, you want to put your most specific statements up top and get more generic as you go down the condition list.
Upvotes: 1
Reputation: 5266
Just change the conditions order
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";
}
}
or even
if ($_POST['cpanel_notifications'] == 1){
$type = ($_POST['remote_server'] == 1?"fm":"m");
}
elseif($_POST['cpanel_notifications'] == 0){
$type = ($_POST['remote_server'] == 0?"fnm":"nm");
}
Upvotes: 1
Reputation: 19888
what you need todo is reorder your if's
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";
}
Upvotes: 2
Reputation: 17759
This is because the first if
statement is true. There is no reason to go to any of the elses
.
Upvotes: 7