What are some potential pitfalls to consider when using if-else statements in PHP to check for multiple time ranges?
One potential pitfall when using if-else statements to check for multiple time ranges in PHP is that the code can become lengthy and difficult to maintain if there are many time ranges to check. One way to solve this issue is to use a switch statement instead, which can make the code more readable and manageable.
// Check for time ranges using a switch statement
$currentTime = date("H:i");
switch (true) {
case ($currentTime >= "06:00" && $currentTime < "12:00"):
echo "Good morning!";
break;
case ($currentTime >= "12:00" && $currentTime < "18:00"):
echo "Good afternoon!";
break;
case ($currentTime >= "18:00" && $currentTime < "22:00"):
echo "Good evening!";
break;
default:
echo "Good night!";
}