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










-2















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?



<!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>









share|improve this question

















  • 3





    You are comparing (seemingly) numbers to a string, take the quotes away, e.g $currentHour > 21, and you should use an || (or) operator instead of && (and)

    – GrumpyCrouton
    Nov 13 '18 at 21:41







  • 4





    Even then the value can't be larger than 21 but smaller than 5 at the same time.

    – mario
    Nov 13 '18 at 21:42











  • @GrumpyCrouton ok, I'll concede that, but still, string comparison of numbers?!

    – j08691
    Nov 13 '18 at 21:45











  • @j08691 I agree, it may be improper and could possibly lead to some edge cases where it doesn't work right - I used to be completely against using w3schools too, but they have gotten better more recently, and they are a huge resource for new programmers

    – GrumpyCrouton
    Nov 13 '18 at 21:46











  • I see it running correctly. At what time of the day were you testing?

    – icortesi
    Nov 13 '18 at 21:47
















-2















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?



<!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>









share|improve this question

















  • 3





    You are comparing (seemingly) numbers to a string, take the quotes away, e.g $currentHour > 21, and you should use an || (or) operator instead of && (and)

    – GrumpyCrouton
    Nov 13 '18 at 21:41







  • 4





    Even then the value can't be larger than 21 but smaller than 5 at the same time.

    – mario
    Nov 13 '18 at 21:42











  • @GrumpyCrouton ok, I'll concede that, but still, string comparison of numbers?!

    – j08691
    Nov 13 '18 at 21:45











  • @j08691 I agree, it may be improper and could possibly lead to some edge cases where it doesn't work right - I used to be completely against using w3schools too, but they have gotten better more recently, and they are a huge resource for new programmers

    – GrumpyCrouton
    Nov 13 '18 at 21:46











  • I see it running correctly. At what time of the day were you testing?

    – icortesi
    Nov 13 '18 at 21:47














-2












-2








-2








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?



<!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>









share|improve this question














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?



<!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>






php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 21:40









PuppyD13PuppyD13

44




44







  • 3





    You are comparing (seemingly) numbers to a string, take the quotes away, e.g $currentHour > 21, and you should use an || (or) operator instead of && (and)

    – GrumpyCrouton
    Nov 13 '18 at 21:41







  • 4





    Even then the value can't be larger than 21 but smaller than 5 at the same time.

    – mario
    Nov 13 '18 at 21:42











  • @GrumpyCrouton ok, I'll concede that, but still, string comparison of numbers?!

    – j08691
    Nov 13 '18 at 21:45











  • @j08691 I agree, it may be improper and could possibly lead to some edge cases where it doesn't work right - I used to be completely against using w3schools too, but they have gotten better more recently, and they are a huge resource for new programmers

    – GrumpyCrouton
    Nov 13 '18 at 21:46











  • I see it running correctly. At what time of the day were you testing?

    – icortesi
    Nov 13 '18 at 21:47













  • 3





    You are comparing (seemingly) numbers to a string, take the quotes away, e.g $currentHour > 21, and you should use an || (or) operator instead of && (and)

    – GrumpyCrouton
    Nov 13 '18 at 21:41







  • 4





    Even then the value can't be larger than 21 but smaller than 5 at the same time.

    – mario
    Nov 13 '18 at 21:42











  • @GrumpyCrouton ok, I'll concede that, but still, string comparison of numbers?!

    – j08691
    Nov 13 '18 at 21:45











  • @j08691 I agree, it may be improper and could possibly lead to some edge cases where it doesn't work right - I used to be completely against using w3schools too, but they have gotten better more recently, and they are a huge resource for new programmers

    – GrumpyCrouton
    Nov 13 '18 at 21:46











  • I see it running correctly. At what time of the day were you testing?

    – icortesi
    Nov 13 '18 at 21:47








3




3





You are comparing (seemingly) numbers to a string, take the quotes away, e.g $currentHour > 21, and you should use an || (or) operator instead of && (and)

– GrumpyCrouton
Nov 13 '18 at 21:41






You are comparing (seemingly) numbers to a string, take the quotes away, e.g $currentHour > 21, and you should use an || (or) operator instead of && (and)

– GrumpyCrouton
Nov 13 '18 at 21:41





4




4





Even then the value can't be larger than 21 but smaller than 5 at the same time.

– mario
Nov 13 '18 at 21:42





Even then the value can't be larger than 21 but smaller than 5 at the same time.

– mario
Nov 13 '18 at 21:42













@GrumpyCrouton ok, I'll concede that, but still, string comparison of numbers?!

– j08691
Nov 13 '18 at 21:45





@GrumpyCrouton ok, I'll concede that, but still, string comparison of numbers?!

– j08691
Nov 13 '18 at 21:45













@j08691 I agree, it may be improper and could possibly lead to some edge cases where it doesn't work right - I used to be completely against using w3schools too, but they have gotten better more recently, and they are a huge resource for new programmers

– GrumpyCrouton
Nov 13 '18 at 21:46





@j08691 I agree, it may be improper and could possibly lead to some edge cases where it doesn't work right - I used to be completely against using w3schools too, but they have gotten better more recently, and they are a huge resource for new programmers

– GrumpyCrouton
Nov 13 '18 at 21:46













I see it running correctly. At what time of the day were you testing?

– icortesi
Nov 13 '18 at 21:47






I see it running correctly. At what time of the day were you testing?

– icortesi
Nov 13 '18 at 21:47













2 Answers
2






active

oldest

votes


















3














Replace && with ||.



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






share|improve this answer
































    -1














    Ok managed to figure out my newbie problem, thank you very much for your prompt assistance and help. The if statement was not being reached because I wrote a time that can never be reached. However I wrote a more "simple" syntax which seem to work well.



    $currentHour = Date("G");
    if ($currentHour <= 5)

    echo '<div class="item1" id="night">';

    elseif ($currentHour <= 7)

    echo '<div class="item1" id="twilight">';

    elseif ($currentHour <= 19)

    echo '<div class="item1" id="day">';

    elseif ($currentHour <= 21)

    echo '<div class="item1" id="twilight">';

    elseif ($currentHour > 21)
    echo '<div class="item1" id="night">';






    share|improve this answer























    • Your first scrpt is better, the problem was with the and operators, they needed to be or.

      – icortesi
      Nov 13 '18 at 22:20






    • 1





      This is how I would do it, only one echo needed. pastiebin.com/5beb4fee81b99

      – dmikester1
      Nov 13 '18 at 22:30











    • @dmikester1 Love your code!

      – PuppyD13
      Nov 13 '18 at 22:35











    • Made it cleaner based on your first post.

      – dmikester1
      Nov 13 '18 at 22:43










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289925%2fphp-operators-in-an-if-elseif-else-statement-jumping-to-else%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    Replace && with ||.



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






    share|improve this answer





























      3














      Replace && with ||.



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






      share|improve this answer



























        3












        3








        3







        Replace && with ||.



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






        share|improve this answer















        Replace && with ||.



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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 22:09









        miken32

        23.7k84972




        23.7k84972










        answered Nov 13 '18 at 21:47









        user9964312user9964312

        583




        583























            -1














            Ok managed to figure out my newbie problem, thank you very much for your prompt assistance and help. The if statement was not being reached because I wrote a time that can never be reached. However I wrote a more "simple" syntax which seem to work well.



            $currentHour = Date("G");
            if ($currentHour <= 5)

            echo '<div class="item1" id="night">';

            elseif ($currentHour <= 7)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour <= 19)

            echo '<div class="item1" id="day">';

            elseif ($currentHour <= 21)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour > 21)
            echo '<div class="item1" id="night">';






            share|improve this answer























            • Your first scrpt is better, the problem was with the and operators, they needed to be or.

              – icortesi
              Nov 13 '18 at 22:20






            • 1





              This is how I would do it, only one echo needed. pastiebin.com/5beb4fee81b99

              – dmikester1
              Nov 13 '18 at 22:30











            • @dmikester1 Love your code!

              – PuppyD13
              Nov 13 '18 at 22:35











            • Made it cleaner based on your first post.

              – dmikester1
              Nov 13 '18 at 22:43















            -1














            Ok managed to figure out my newbie problem, thank you very much for your prompt assistance and help. The if statement was not being reached because I wrote a time that can never be reached. However I wrote a more "simple" syntax which seem to work well.



            $currentHour = Date("G");
            if ($currentHour <= 5)

            echo '<div class="item1" id="night">';

            elseif ($currentHour <= 7)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour <= 19)

            echo '<div class="item1" id="day">';

            elseif ($currentHour <= 21)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour > 21)
            echo '<div class="item1" id="night">';






            share|improve this answer























            • Your first scrpt is better, the problem was with the and operators, they needed to be or.

              – icortesi
              Nov 13 '18 at 22:20






            • 1





              This is how I would do it, only one echo needed. pastiebin.com/5beb4fee81b99

              – dmikester1
              Nov 13 '18 at 22:30











            • @dmikester1 Love your code!

              – PuppyD13
              Nov 13 '18 at 22:35











            • Made it cleaner based on your first post.

              – dmikester1
              Nov 13 '18 at 22:43













            -1












            -1








            -1







            Ok managed to figure out my newbie problem, thank you very much for your prompt assistance and help. The if statement was not being reached because I wrote a time that can never be reached. However I wrote a more "simple" syntax which seem to work well.



            $currentHour = Date("G");
            if ($currentHour <= 5)

            echo '<div class="item1" id="night">';

            elseif ($currentHour <= 7)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour <= 19)

            echo '<div class="item1" id="day">';

            elseif ($currentHour <= 21)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour > 21)
            echo '<div class="item1" id="night">';






            share|improve this answer













            Ok managed to figure out my newbie problem, thank you very much for your prompt assistance and help. The if statement was not being reached because I wrote a time that can never be reached. However I wrote a more "simple" syntax which seem to work well.



            $currentHour = Date("G");
            if ($currentHour <= 5)

            echo '<div class="item1" id="night">';

            elseif ($currentHour <= 7)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour <= 19)

            echo '<div class="item1" id="day">';

            elseif ($currentHour <= 21)

            echo '<div class="item1" id="twilight">';

            elseif ($currentHour > 21)
            echo '<div class="item1" id="night">';







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 21:58









            PuppyD13PuppyD13

            44




            44












            • Your first scrpt is better, the problem was with the and operators, they needed to be or.

              – icortesi
              Nov 13 '18 at 22:20






            • 1





              This is how I would do it, only one echo needed. pastiebin.com/5beb4fee81b99

              – dmikester1
              Nov 13 '18 at 22:30











            • @dmikester1 Love your code!

              – PuppyD13
              Nov 13 '18 at 22:35











            • Made it cleaner based on your first post.

              – dmikester1
              Nov 13 '18 at 22:43

















            • Your first scrpt is better, the problem was with the and operators, they needed to be or.

              – icortesi
              Nov 13 '18 at 22:20






            • 1





              This is how I would do it, only one echo needed. pastiebin.com/5beb4fee81b99

              – dmikester1
              Nov 13 '18 at 22:30











            • @dmikester1 Love your code!

              – PuppyD13
              Nov 13 '18 at 22:35











            • Made it cleaner based on your first post.

              – dmikester1
              Nov 13 '18 at 22:43
















            Your first scrpt is better, the problem was with the and operators, they needed to be or.

            – icortesi
            Nov 13 '18 at 22:20





            Your first scrpt is better, the problem was with the and operators, they needed to be or.

            – icortesi
            Nov 13 '18 at 22:20




            1




            1





            This is how I would do it, only one echo needed. pastiebin.com/5beb4fee81b99

            – dmikester1
            Nov 13 '18 at 22:30





            This is how I would do it, only one echo needed. pastiebin.com/5beb4fee81b99

            – dmikester1
            Nov 13 '18 at 22:30













            @dmikester1 Love your code!

            – PuppyD13
            Nov 13 '18 at 22:35





            @dmikester1 Love your code!

            – PuppyD13
            Nov 13 '18 at 22:35













            Made it cleaner based on your first post.

            – dmikester1
            Nov 13 '18 at 22:43





            Made it cleaner based on your first post.

            – dmikester1
            Nov 13 '18 at 22:43

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289925%2fphp-operators-in-an-if-elseif-else-statement-jumping-to-else%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto