Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts

    If statement gives true when it should be false

    PHP Code:
    <?php
    $input 
    4;

    if (
    $input == || 3) {
        echo 
    'true';
    }
    ?>
    The output is true, why?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    || has a lower priority than ==. $input == 2 is false, and since 3 is not considered false, you end up with false || true which results in true.
    I'd assume what you mean is if ($input == 2 || $input == 3).

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    112
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, that's it yes.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Good good.
    If you have many possible matches, you can create an array and use an in_array: if (in_array($input, $aValid)) where $aValid is an array of valids like $aValid = array(2, 3);.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •