PHP operators in an IF…ElseIf…Else statement. Jumping to Else?

Puppy

New Member
#1
I am trying to use the IF...ElseIf...Else statement which includes basic Logical and Comparison operators. When running the code, instead of looping trough the if statement it echos the else statement only. I followed w3schools (https://www.w3schools.com/php/php_operators.asp and https://www.w3schools.com/php/php_if_else.asp) but had no luck. I am kind of confused now, is there a better way of how to do it please?
Mã:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php

    $currentHour = Date("G");

     echo $currentHour;

     if ($currentHour > "21" && $currentHour < "5")
     {
           echo '<div class="item1" id="night">';
     }
     elseif ($currentHour > "7" && $currentHour < "19")
     {
           echo '<div class="item1" id="day">';
     }
     else
     {
           echo '<div class="item1" id="twilight">';
     }
?>

</body>
</html>
 

Admin

Administrator
Thành viên BQT
#2
Replace && with ||.

In your if statement, if $currenthour > 21 then obviously $currenthour < 5 will fail. Similarly with your elseif statement.
 
Top