If variable is not set?

Discussion in 'PHP' started by mokimofiki, Sep 18, 2008.

  1. #1
    How do I say in php if a variable is not set then go to a different page and if it is then continue loading current page?

    (I can do if isset fine but for what i'm doing i need it the other way around)

    I know I should probably know this since it seems like it would be easy I'm just having a bit of trouble with it and figured someone here could tell me quickly and save me a long drawn out process of figuring what i'm doing wrong. Thank you :)
     
    mokimofiki, Sep 18, 2008 IP
  2. lui2603

    lui2603 Peon

    Messages:
    729
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    if(!isset($variable)){
    //do something
    }
    
    PHP:

    the ! means not ;)
     
    lui2603, Sep 18, 2008 IP
  3. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this one:
    
    <?php
    if($anyvariable){
    echo "Variable is present";
    }else{
    echo "Variable is NOT present";
    }
    
    PHP:
     
    mehdi, Sep 19, 2008 IP
  4. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's not right in all cases! Here's an example - if your url is http://test.test/?test
    then
    if($_GET['test'])
    PHP:
    will return false, cause the var has no value, but
    isset($_GET['test'])
    PHP:
    will return true, cause the var is set, though it's null.

    Just wanted to explain a bit further, cause that's important to know :)
     
    xlcho, Sep 19, 2008 IP
    webrickco likes this.
  5. nabz245

    nabz245 Well-Known Member

    Messages:
    677
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #5
    Use:
    if (!isset($varname)) {
    }

    ! gives the opposite effect, so the following means 'not equal to':

    ($var1 != $var2) {
     
    nabz245, Sep 19, 2008 IP
  6. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #6
    I prefer doing stuff on positive side.

    
    if(empty($variable)){
    //do something
    }
    
    PHP:
    Returns FALSE if var has a non-empty and non-zero value.

    The following things are considered to be empty:

    * "" (an empty string)
    * 0 (0 as an integer)
    * "0" (0 as a string)
    * NULL
    * FALSE
    * array() (an empty array)
    * var $var; (a variable declared, but without a value in a class)
     
    Kaizoku, Sep 19, 2008 IP