PHP if else with negative and positive numbers?

Been stuck on a little problem for some time.

I'm getting float (00.00) numbers from a database. Now I want to display them with different backgrounds, depending on the value.

 if($zahl > 0 && $zahl < 10) {}

is not the problem, but as soon as a number becomes negative, I don't get it displayed.

 if($zahl < -10 && $zahl < -20) {}

doesn't work. I want positive numbers from
+ 0 to +20 Green background
+20 to +40 Yellow
+40 to +60 Red
Likewise, negative numbers
– 0.01 to -10 in green
– 10.01 to – 20 in yellow
and everything above -20 is shown in red.
Does anyone have an idea?

(3 votes)
Loading...

Similar Posts

Subscribe
Notify of
8 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Randy870
1 month ago

if($number < -10 && $number < -20) {}

makes no sense. You have to write

if($number < -10 && $number >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= >= -20!

Randy870
1 month ago
Reply to  TrucksLOG

No, I can’t. If $hour=65, the second variant becomes active and the third one no longer comes to the train. You have to program clean or let it be the same… sry

Destranix
1 month ago
if($stunden == 0){
    $farbe = "bg-success";
} elseif ($stunden >= 10 && $stunden <= 19.99) {

Better

if($stunden == 0){
    $farbe = "bg-success";
} elseif ($stunden >= 10 && $stunden < 20.00) {

Otherwise you have a gap between 19.99 and 20.00. (Or, since the floats are, if it's running stupid, it's even the same value in binary representation).

Your two letzen elseifs have the same condition, because something doesn't fit.

To the problem included:

I'd solve it differently. Make an array, write in the values at which the areas adjoin/end:

$borders = [-20, -10, 0, 10, 20, 40, 60]

Then take the values from the array and compare it with:

if($x < $borders[0]){

}elseif($x < $borders[1]){//Greater equal border[0]

}elseif($x < $borders[2]){//Greater equal border[1]

}else{//Greater equal last border

}
Pixelated
1 month ago

No, I can’t.

The range 40 to 50 is not covered at all.

You also have overlaps in the negative area

elseif ($stunden <= -10 && $stunden > -19.99) {
    $farbe = "bg-warning";
} elseif ($stunden <= -10 && $stunden > -19.99) {
    $farbe = "bg-danger";
}
Pixelated
1 month ago

Programming has nothing to do with faith. Your code from your comment will only produce exactly two states: bg-success and big-warning

There are figures that are not achieved with the query at all. What about the 15?