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
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts

    more concise way of avoiding unpredefined error messages...?

    This works:

    PHP Code:
    if (!isset($_SESSION['logged_in']) || (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == "no")) {
         
    show login form;

    But it seems so verbose. Is there a simpler way I'm missing?

    (BTW, I need to be able to detect the dfference between 'was logged in during this session but has now logged out' and 'hasn't ever logged in'. Hence, the need to differentiate between an undefined $_SESSION['logged_in'], and $_SESSION['logged_in'] = "no").

    Thanks

  • #2
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    if (empty($_SESSION['logged_in']) || $_SESSION['logged_in'] == "no") {
         
    show login form;

    You could do it just like that

  • #3
    Regular Coder
    Join Date
    Feb 2007
    Location
    London
    Posts
    225
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Ah!

    Thanks a lot.
    :-)

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    It's better to use isset() (if possible, always use the more logical sounding/looking method of doing something, as it improves your code's readability).

    PHP Code:
    if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] == "no") {
         
    show login form;

    The reason you don't need a second isset() is that || and && are short circuited by PHP. Meaning that in an Or (||) test, if one of them is true (the first part), you don't care about the second part anymore. So if logged_in is not set as a session variable, it won't check the other part of the test. So you don't have to worry about any warnings/errors.

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    empty() is identical to isset() but also returns true if the variable has no value. (false or nothing)

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Correct, but that is not the reason I gave. (And that variable should not be empty at any time, so the additional check is not useful)

    always use the more logical sounding/looking method of doing something, as it improves your code's readability
    What I meant by this is that someone who reads the empty() variation might not realize it is doing the same as isset().

    If using one function over the other makes no functional difference, but the naming or use of that function makes it easier to read your code (as in: more like normal language), then I'd suggest using the one that reads most logical.

    Clean code is an important element that few tutorials out there teach, and books don't always spend enough time on it.

  • Users who have thanked aedrin for this post:

    cfructose (09-14-2007)


  •  

    Posting Permissions

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