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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts

    Question Why won't this work?

    I'm trying to check three conditions using one if statement. I've included the "!" to check if something doesn't match what I want.

    Here's what I have:

    Code:
    if (!isset($_SESSION['login']) && !strlen($key) == 128 && !$key == $_SESSION['login']){
    //do something
    }
    Do I have to use "||" instead of "&&", or can I somehow still use "&&"?

    Thanks for the help!

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You want to use "!=" to check for not equal.

    PHP Code:
    if (!isset($_SESSION['login']) && strlen($key) != 128 && $key != $_SESSION['login']){ 

  • Users who have thanked Fumigator for this post:

    four0four (02-24-2010)

  • #3
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    You want to use "!=" to check for not equal.

    PHP Code:
    if (!isset($_SESSION['login']) && strlen($key) != 128 && $key != $_SESSION['login']){ 
    Thanks, I see! So I was doing it totally backwards.

    For some reason though, it still doesn't execute my IF statement if just one of the conditions doesn't match.

    If none of the conditions match, then it executes, but if just one matches then it never executes.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This isn't right.
    PHP Code:
    if (!isset($_SESSION['login']) && strlen($key) != 128 && $key != $_SESSION['login']){ 
    If $_SESSION['login'] DOESN'T exist AND $key != 128 AND $key != $_SESSION['login'] (which err.... doesn't exist at this point since we ensured that in the first expression.....).

    Looks to me like this should be:
    PHP Code:
    if (!isset($_SESSION['login']) || strlen($key) != 128 || $key != $_SESSION['login']) 
    So in phrase format, if you do not have a session login OR the session login doesn't match the key OR the key is not 128 characters in length, execute this code block. Personally, I can't see you needing the strlen check in there at all, I would presume that $key is coded somewhere and loaded, not provided by the client. Also, look into using strcmp for you're comparisons instead of == and !=. PHP is a string based language so it will support the comparison, but you will find most other common mainstream langauages do not.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    four0four (02-24-2010)

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Using the &&, you're saying that all three conditions have to be true, (false), for the code within the block to execute. If you want the code to run if only one or more of the conditions are true, (false), use ||.
    Last edited by MattF; 02-24-2010 at 11:20 PM.

  • Users who have thanked MattF for this post:

    four0four (02-24-2010)

  • #6
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Thanks everyone! That fixed it!


  •  

    Posting Permissions

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