SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Syntax help with sessions comparison

    Hello all - first query of the week!

    What would be the syntax for comparing data against two different session variables? I can't seem to get it quite right.

    What I want it to do is check there's a logged in session and check whether the email session is blank if so echo something:

    PHP Code:
    If (isset($_SESSION['loggedIn'])) && ($_SESSION['email']) != '')
      {
       echo 
    'blah';
      } 
    The first comparison is fine but can't get the second bit right.
    Ta!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Morning.

    PHP Code:
    <?php
    if((isset($_SESSION['loggedIn']) === true) && (empty($_SESSION['email']) === false))
    {
        
    // do stuff
    }
    ?>
    See empty().
    Last edited by AnthonySterling; May 17, 2009 at 23:58.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    u were on the right track

    PHP Code:
    If (isset($_SESSION['loggedIn'])) && ($_SESSION['email']) == '')
      {
       echo 
    'blah';
      } 

  4. #4
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow that was quick!

    Thank you both - I'm sure one or both will do the trick.

    Is there any advantage to using empty() rather than checking is something doesn't equal ' '? Or is it just a faster, neater way of doing the same thing?

    Edit: oooh... not seen === before... (goes to look it up).
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  5. #5
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    === means of same variable type as well as same value.

    empty() or ==''

    will give the same result. my way is quicker to type. the other way is more textbook.

  6. #6
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Reposting the old "is it empty or set or is that null?" checklist, just so maybe a few more people will see it and then adopt " === " style checking and use var_dump() before doing any conditional code forks.

    Hoping this helps you retain some sanity as you PHP.

  8. #8
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    empty() returns TRUE for the string '0' so it's not the same as comparing to an empty string:

    PHP Code:
    $foo '0';

    if (empty(
    $foo)) { // TRUE
        
    echo 'EMPTY';
    }

    if (
    $foo == '') { // FALSE
        
    echo 'EMPTY';

    Last edited by decowski; May 18, 2009 at 04:55.


Bookmarks

Posting Permissions

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