What’s interesting to me is that a few of these checks are redundant:
if ($h >= 0 && $h < 4) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
// ... more checks here
else if ($h >= 19) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
else if ($h < 0) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
So basically we can combine $h >= 0 && $h < 4
and $h < 0
into one, to become:
if ($h < 4) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
So then we have
if ($h < 4) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
// ... more checks here
else if ($h >= 19) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
And we can combine that into one again
if ($h < 4 || $h >= 19) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
// ... more checks here
so then the entire list becomes:
if ($h < 4 || $h >= 19) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
else if ($h >= 4 && $h < 8) $dynamicaudi = './uploads/herotime/auditt_4-8.jpg';
else if ($h >= 8 && $h < 10) $dynamicaudi = './uploads/herotime/auditt_8-10.jpg';
else if ($h >= 10 && $h < 15) $dynamicaudi = './uploads/herotime/auditt_10-15.jpg';
else if ($h >= 15 && $h < 16) $dynamicaudi = './uploads/herotime/auditt_15-16.jpg';
else if ($h >= 16 && $h < 17) $dynamicaudi = './uploads/herotime/16-17.jpg';
else if ($h >= 17 && $h < 19) $dynamicaudi = './uploads/herotime/auditt_17-19.jpg';
What’s interesting now is that on the second line, $h >= 4
is always already true, because otherwise the first if
would have matched, so that line can be simplified to:
else if ($h < 8) $dynamicaudi = './uploads/herotime/auditt_4-8.jpg';
And the same thing goes for all the checks, so the final result becomes:
if ($h < 4 || $h >= 19) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
else if ($h < 8) $dynamicaudi = './uploads/herotime/auditt_4-8.jpg';
else if ($h < 10) $dynamicaudi = './uploads/herotime/auditt_8-10.jpg';
else if ($h < 15) $dynamicaudi = './uploads/herotime/auditt_10-15.jpg';
else if ($h < 16) $dynamicaudi = './uploads/herotime/auditt_15-16.jpg';
else if ($h < 17) $dynamicaudi = './uploads/herotime/16-17.jpg';
else if ($h < 19) $dynamicaudi = './uploads/herotime/auditt_17-19.jpg';
In this result I don’t really like the $h >= 19
at the start anymore. All lines look the same, except for that first one. Let’s put it back at the end to see what happens:
if ($h < 4) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
else if ($h < 8) $dynamicaudi = './uploads/herotime/auditt_4-8.jpg';
else if ($h < 10) $dynamicaudi = './uploads/herotime/auditt_8-10.jpg';
else if ($h < 15) $dynamicaudi = './uploads/herotime/auditt_10-15.jpg';
else if ($h < 16) $dynamicaudi = './uploads/herotime/auditt_15-16.jpg';
else if ($h < 17) $dynamicaudi = './uploads/herotime/16-17.jpg';
else if ($h < 19) $dynamicaudi = './uploads/herotime/auditt_17-19.jpg';
else if ($h >= 19) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
Right, but then we don’t need the if
anymore, since it’s just the default case now. So:
if ($h < 4) $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
else if ($h < 8) $dynamicaudi = './uploads/herotime/auditt_4-8.jpg';
else if ($h < 10) $dynamicaudi = './uploads/herotime/auditt_8-10.jpg';
else if ($h < 15) $dynamicaudi = './uploads/herotime/auditt_10-15.jpg';
else if ($h < 16) $dynamicaudi = './uploads/herotime/auditt_15-16.jpg';
else if ($h < 17) $dynamicaudi = './uploads/herotime/16-17.jpg';
else if ($h < 19) $dynamicaudi = './uploads/herotime/auditt_17-19.jpg';
else $dynamicaudi = './uploads/herotime/auditt_0-4.jpg';
So now we have a number and the filename of an image for each hour. What if I stuff that in an array?
$images = [
4 => './uploads/herotime/auditt_0-4.jpg',
8 => './uploads/herotime/auditt_4-8.jpg',
10 => './uploads/herotime/auditt_8-10.jpg',
15 => './uploads/herotime/auditt_10-15.jpg',
16 => './uploads/herotime/auditt_15-16.jpg',
17 => './uploads/herotime/16-17.jpg',
19 => './uploads/herotime/auditt_17-19.jpg',
];
and now loop it:
$image = './uploads/herotime/auditt_0-4.jpg'; // default
foreach ($images as $hour => $imagePath) {
if ($h < $hour) {
$image = $imagePath;
break;
}
}
I don’t know about you, but to me that looks a lot simpler and easier to maintain when needing to add more hours etc