Results 1 to 4 of 4

Thread: help with breaking down isset statement

  1. #1
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with breaking down isset statement

    I'm not sure what this statement is supposed to do:

    $action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';
    Immediately after that there is a switch function:

    switch ($action) {
    case 'add' :
    addToCart();
    break;
    case 'update' :
    updateCart();
    break;
    case 'delete' :
    deleteFromCart();
    break;
    case 'view' :
    What does the isset statement do here?

    Thanks for any help!

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    a ? b : c
    is read as
    Code:
    if(a) b; else c;
    although the end result is slightly different, because it can be used in expressions.
    It checks to see if $_GET['action'] exists and has content. If so, it sets $action to it. Otherwise, it sets $action to "view".
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Jacquih
    I'm not sure what this statement is supposed to do:

    Code:
    $action = (isset($_GET['action']) && $_GET['action'] != '') ? $_GET['action'] : 'view';
    As you should be aware, the $_GET superglobal (associative) array contains name/value pairs present in the query string, and attempting to access an array element that doesn't exist generates a warning.

    The isset function checks to see if a given element has been set to a value, returning true if it has. The following expression checks to make sure that the value isn't an empty string. Finally, the conditional expression (the ? : operator) evaluates to the value of $_GET['action'] if the other two expressions evaluate to true, or the string 'view' otherwise.

    Hope that helps,
    Mike

  4. #4
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default thanks

    It helps alot! Thanks guys!

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
  •