Fork me on GitHub

Checking if a Variable is Set  Bottom

  • I'm wondering if there is a way to check and see if a variable is declared or not, and if not, to set it.

    Code

    if (!defined($country)) {
    $country="0";
    }


    Probably the above won't work, though I have yet to actually test it. Does anyone have any tips for me as to what I can try?

    Thanks in advance for any assistance.
  • Code

    if (!isset($Var)) {
    $Var = 'SomeValue';
    }

    ...Or, you could also do:

    Code

    if ($Var == '') {
    $Var = 'SomeValue';
    }
  • LOL I like the second one:

    if ($Var == "") {
    $Var = 'SomeValue';
    }

    it is so... logical LOL

    Now I wonder, what if you actually wanted to set a var as "" - ie nothing - would isset return false here ?

    eg

    $var=""; // I have "set" this var as "" so I have actually set the var !

    if isset returns false here I think it is illogical and isset should be changed to isvar, ie is there a var?

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • isset will return true even if empty.. empty() is the equivilent of $var == ''

    $var = '';

    if(empty($var)){
    // var is empty
    }

    if(isset($var)){
    // true, $var exists
    }

    isset I think was intended to be based on parameters index.php?module=this .. "module" being the parameter, "this" being the variable or value ..even without "this" the parameter module 'is set' ..

    -IR
  • Interestingly, it will return TRUE. Try it out:

    Code

    <?php

        $Var = '';
       
        if (!isset($Var))
        {
            echo '$Var not set';
        }
        else
        {
            echo '$Var is set';
        }
    ?>
  • ..they're often used in conjunction:

    $var = '';

    if(isset($var) AND !empty($var)){ // parameter is set, but has a null/0 value, so kill it
    echo 'all is well';
    } else {
    die 'no value..kill it.';
    }

    ps: typically defined() is used for constants.. as we do with languages and settings in the API
  • Thanks guys. Got it working, but was away for the weekend so couldn't respond.
  • No prob....thanks for getting back.
  • alarconcepts


    Interestingly, it will return TRUE. Try it out:


    The reason a variable set to '' is considered set is probably because it is not NULL. Empty and NULL are not the same thing. I guess the real test is whether the variable exists, not so much if the variable has a value.

    Frank
  • php.net


    empty() returns FALSE if var has a non-empty and non-zero value. In otherwords, "", 0, "0", NULL, FALSE, array(), and var $var; are all considered empty.

    http://us2.php.net/empty

    php.net

    isset -- Determine whether a variable is set

    (..regardless of value.)
    http://us2.php.net/isset
  • In my oppinion, this is so much ado about nothing.

    I'm sorry I couldn't resist.

    Slugger
  • alarconcepts

    Interestingly, it will return TRUE.


    As well it should, if you want to know if it's been defined as anything, then use isset, if you want to know if it's set to a certian value, use the other method.

    --
    Home Page | Find on Facebook | Follow on Twitter
  • 0 users

This list is based on users active over the last 60 minutes.