I have a certain conditional like below.
>80 category A
71-80 category AB
51-70 category B
41-50 category BC
<40 category C
I need to make a code that meet above condition. But only with 1 if 2 else if 1 else
I figured out that i need to set the default category value, but it still does not meet the expected result
$category= "C";
$value = "10";
if ($value > 80) {
$category = "A";
}else if ($value <= 70 AND $value > 50) {
$category = "B";
}else if ($value <= 80 AND $value > 70) {
$category = "AB";
}else{
$category = "BC";
}
echo $category;
i want to make output like this
90 = A
72 = AB
55 = B
45 = BC
10 = C
but my code show below
90 = A
72 = AB
55 = B
45 = BC
10 = BC
If the point is to limit the keywords if, else and else if, you can use ternary operator in the last else.
function getCat($val)
{
$cat = '';
if ($val >= 81) {
$cat = 'A';
} else if ($val >= 71 && $val <= 80) {
$cat = 'AB';
} else if ($val >= 51 && $val <= 70) {
$cat = 'B';
} else {
$cat = $val < 40 ? 'C' : 'BC';
}
return $cat;
}
Here's a demo
There's one issue though. If the value is 40, this would give it BC, but you never wrote any rule for that number so it is what it is
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With