Help with OR logic operator

Discussion in 'PHP' started by cigi, Jul 5, 2008.

  1. #1
    Hi guys

    I'm new (ish) to PHP and have a problem with the below code:

    <?php
    $total = $_GET['total'];

    if ($total <=5){
    echo "<b>You Fail</b> <br><br>";
    }
    elseif ($total == 6 or 7){
    echo "<b>Not great, but not bad</b> <br><br>";
    }
    else{
    echo "<b>Nice work</b> <br><br>";
    }
    echo "Test Complete, you correctly answered $total<br><a href='index.php?start=1'>Test Again</a>";

    ?>

    When the script runs and $total is 5 or below "You Fail" is echo'ed.

    If $total is 6 or 7 "Not great, but not bad" is echo'ed as expected.

    BUT if $total is 8 or higher "Not great, but not bad" is still echo'ed but I wanted "Nice work" printed, because $total is not lower 5 or lower nor is it 6 or 7, so I though the script would echo whats in the "else" section.

    Can someone help please!!

    cigi
     
    cigi, Jul 5, 2008 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    you can try this

    <?php
    $total = $_GET['total'];

    if ($total <=5){
    echo "<b>You Fail</b> <br><br>";
    }
    elseif ($total <= 7){
    echo "<b>Not great, but not bad</b> <br><br>";
    }
    else{
    echo "<b>Nice work</b> <br><br>";
    }
    echo "Test Complete, you correctly answered $total<br><a href='index.php?start=1'>Test Again</a>";

    ?>
     
    serialCoder, Jul 5, 2008 IP
  3. cigi

    cigi Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    WOW quick reply thanks serialCoder.

    Yep that works perfectly.

    Many thanks

    cigi
     
    cigi, Jul 5, 2008 IP
  4. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #4
    no worries :)

    if you're wondering why your code was not working before, on this line

    elseif ($total == 6 or 7){

    you should have written it this way

    $total == 6 OR $total == 7)

    hope that helps :)
     
    serialCoder, Jul 5, 2008 IP
  5. cigi

    cigi Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks again serialCoder :)

    I was going to ask why it did not work, I see how it works now, stating the variable for each value.

    But I think I will leave as <=7 though, seems to be the best way to code it as you advised.

    Many thanks!!

    cigi
     
    cigi, Jul 5, 2008 IP