I have a simple PHP script that is checking the range of a number. For some reason, once the number I am checking equals 100%
the code does not work. Here is my code:
$percent_completed = '100%';
if ($percent_completed <= '20%') {
$color = 'danger';
} elseif ($percent_completed >= '21%' && $percent_completed < '40%') {
$color = 'warning';
} elseif ($percent_completed >= '40%' && $percent_completed < '60%') {
$color = 'info';
} elseif ($percent_completed >= '60%' && $percent_completed < '80%') {
$color = 'primary';
} elseif ($percent_completed >= '80%' && $percent_completed < '100%') {
$color = 'default';
} else {
$color = 'success';
}
echo $color;
All of the conditional checks above work just fine until $percent_completed
is equal to 100%
. For some reason it is set to 100%
the $color
that is printed out is danger
. I am sure it is a simple fix but everything I have tried does not work.
Get rid of the %
from your $percent_completed
variable. It makes it as string which will give you different results than when comparing as a integer (for numbers).
$percent_completed = 100;
if ($percent_completed <= 20) {
$color = 'danger';
} elseif ($percent_completed < 40) {
$color = 'warning';
} elseif ($percent_completed < 60) {
$color = 'info';
} elseif ($percent_completed < 80) {
$color = 'primary';
} elseif ($percent_completed < 100) {
$color = 'default';
} else {
$color = 'success';
}
echo $color;
You're doing calculations on a string.
That means that '2%' is actually higher than '100%' (for example).
Remove the percentage symbol and use it when required during output.
You could simplify this considerably.
if
statement correctly and exits the condition. Stack from top to bottom, and you use 1/2 the code. (Or you could write it bottom to top and use >
instead).$p = 100;
if ($p == 100)
$color = "success";
elseif ($p >= 80)
$color = "default";
elseif ($p >= 60)
$color = "primary";
elseif ($p >= 40)
$color = "info";
elseif ($p > 20)
$color = "warning";
elseif ($p <= 20)
$color = "danger";
echo $color;
Just to point out another approach, I'm posting an alternative solution. Sometimes it's more readable to use a simple algebric formula instead of plenty of if-else conditions.
//Assign current percent value to a variable
$percent_completed = 100;
//Assign an array of all notifications
$array_notifications = array("danger", "warning", "info", "primary", "default", "success");
//Calculate index of current notification
$current_index = floor($percent_completed / 20);
//Print or do something else with detected notification type
echo $array_notifications[$current_index];
Comparing percentage strings could work if you're using a natural-order compare function.
Natural order compare functions separate strings and numbers and will treat the numbers as numbers rather than string.
So instead of:
[ "1", "10", "2" ]
You will get:
[ "1", "2", "10" ]
Some languages have this function built in (like PHP: strnatcmp) but JavaScript sadly does not. Writing your own implementation is not very hard, but it's not very easy either.
In this case I would definitely recommend simplifying (like John's solution).