Using the $_GET function

Discussion in 'PHP' started by chriswick, Dec 17, 2007.

  1. #1
    Is this code right for using a default variable, it's just I want it to use a default if it can't find the one from E.g. /index.php?id1=67&id2=78 it will use id2 if it can't find ID1?

    <?php echo $_GET['id1']; if (!empty($_GET['id2'])) ?>
    PHP:
    ID1 being the one it's looking for and ID2 being the one it uses if it can't find ID one?

    Is that right?

    Thanks for your help!
     
    chriswick, Dec 17, 2007 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    if (!empty($_GET['id1'])) {
     echo $_GET['id1']) 
    }
    elseif (!empty($_GET['id2'])) {
     echo $_GET['id2']) 
    }
    ?>
    PHP:
     
    matthewrobertbell, Dec 17, 2007 IP
  3. chriswick

    chriswick Peon

    Messages:
    907
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks do you know how I could get ID2 without having in the URL?
     
    chriswick, Dec 17, 2007 IP
  4. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Use $_POST instead of $_GET :)
     
    Noddegamra, Dec 17, 2007 IP
  5. chriswick

    chriswick Peon

    Messages:
    907
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Then should I put the value I want instead of the ID2?
     
    chriswick, Dec 17, 2007 IP
  6. Noddegamra

    Noddegamra Peon

    Messages:
    1,013
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That would probably be best actually. If ID2 is a set default value for all, then yes :)

    example:

    
    <?php
    if (!empty($_GET['id1'])) {
     $var =  $_GET['id1'];
    }
    else {
     $var =  "78"; //Value of ID2 (example)
    }
    echo "id number value = $var";
    
    ?>
    
    PHP:
     
    Noddegamra, Dec 17, 2007 IP
    chriswick likes this.
  7. chriswick

    chriswick Peon

    Messages:
    907
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7

    Ok, thanks for your help!
     
    chriswick, Dec 17, 2007 IP