SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm in the habit of verfying the existance of a value in a variable using the following

    if ($var)

    But, most tutorials and scripts I've read do the same thing using

    if (isset($var))

    Is isset necessary or are these two examples accomplishing the same thing?

  2. #2
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would say that depends on the variable type.. for example, if $var is a boolean or an integer, that might not work. example:

    Code:
    $test = 0;
    if ($test) {
       echo "variable is set";
    } else {
       echo "variable is not set";
    }
    Even though $test has a value of zero, it would fail this test of whether it is set or not. Likewise, if the variable is a boolean and is set to false, it would fail this test too.

  3. #3
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what would the following do differently?

    $test = 0;
    if (isset($test)) {
    echo "variable is set";
    } else {
    echo "variable is not set";
    }

  4. #4
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By setting $test to 0, if (isset($test)) would return true (i.e., yes, $test has a value), while if ($test) would return false, since the latter tests to see if the value is non-zero.

  5. #5
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In other words, what I'm saying is that if you want to check if a variable has a value or not, you should use isset just to be safe. That way, checking numeric variables won't be problematic when a user gives a zero value.

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cokeman nailed it.

    Here's an example: when creating a forum using vBulletin's Control Panel, you have an option to allow threads in a forum, or not allow threads (which makes it act as a category) - if you allow threads, the value in the database for that field is "1" - vBulletin's script likely then simply uses a statement like this:

    if ($cancontainthreads) {
    // do stuff
    }

    If you choose no, the value is set to 0. Using isset(), both 0 and 1 return true.

  7. #7
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you ever ask a question then when you get the answer wish you never asked it in the first place. With this new knowledge, now I have to determine if I should change these references in 50+ pages.

    Thanks guys. Maybe someone will read this that is just starting their site. They will truly be thankful.

  8. #8
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was just doing some testing and discovered that using isset() to verify that a value is entered in a form field passes a value of true if nothing is entered in the form field. So, I see the value of isset for numerics by accepting the value of 0 but not for character strings because it allows white space.

    I'm I right with this statement or am I missing something?

  9. #9
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a test script I wrote and the results;
    PHP Code:
    <?php // foo.php
    //---------------------------------------
    // $foo declared but not assigned a value
    $foo;

    if (
    $foo) { echo "if(\$foo) is true <br>"; }
     else { echo 
    "if(\$foo) is false <br>"; }

    if(isset(
    $foo)) { echo"isset(\$foo) is tr <br>"; }
    else { echo 
    "isset(\$foo) is false <br>"; }
    echo 
    "<br>";
    //---------------------------------------
    // $bar not declared 

    if ($bah) { echo "if(\$bah) is true <br>"; }
     else { echo 
    "if(\$bah) is false <br>"; }

    if(isset(
    $bah)) { echo"isset(\$bah) is true <br>"; }
    else { echo 
    "isset(\$bah) is false <br>"; }
    echo 
    "<br>";
    //---------------------------------------
    // $baz declared and assigned NULL
    $baz NULL;

    if (
    $baz) { echo "if(\$baz) is true <br>"; }
     else { echo 
    "if(\$baz) is false <br>"; }

    if(isset(
    $baz)) { echo"isset(\$baz) is true <br>"; }
    else { echo 
    "isset(\$baz) is false <br>"; }
    echo 
    "<br>";
    //---------------------------------------
    // $quux declared and assigned empty string
    $quux "";

    if (
    $quux) { echo "if(\$quux) is true <br>"; }
     else { echo 
    "if(\$quux) is false <br>"; }

    if(isset(
    $quux)) { echo"isset(\$quux) is true <br>"; }
    else { echo 
    "isset(\$quux) is false <br>"; }

    if(
    strcmp($quux"") == 0) { echo "if(strcmp($quux, \"\") == 0) is true <br>"; }

    echo 
    "<br>";
    //---------------------------------------
    // $quuux declared and assigned 0
    $quuux 0;

    if (
    $quuux) { echo "if(\$quuux) is true <br>"; }
     else { echo 
    "if(\$quuux) is false <br>"; }

    if(isset(
    $quuux)) { echo"isset(\$quuux) is true <br>"; }
    else { echo 
    "isset(\$quuux) is false <br>"; }
    echo 
    "<br>";
    //---------------------------------------
    // $quuuux declared and assigned false
    $quuuux false;

    if (
    $quuuux) { echo "if(\$quuuux) is true <br>"; }
     else { echo 
    "if(\$quuuux) is false <br>"; }

    if(isset(
    $quuuux)) { echo"isset(\$quuuux) is true <br>"; }
    else { echo 
    "isset(\$quuuux) is false <br>"; }
    echo 
    "<br>";
    //---------------------------------------
    // $foobar[0] and foobar[2] are assigned values
    // what's the status of foobar[1] ?
    $foobar[0] = "cat"$foobar[2] = "mat";

    if (
    $foobar[1]) { echo "if(\$foobar[1]) is true <br>"; }
     else { echo 
    "if(\$foobar[1]) is false <br>"; }

    if(isset(
    $foobar[1])) { echo"isset(\$foobar[1]) is true <br>"; }
    else { echo 
    "isset(\$foobar[1]) is false <br>"; }
    echo 
    "<br>";
    //---------------------------------------
    ?>
    if($foo) is false
    isset($foo) is false

    if($bah) is false
    isset($bah) is false

    if($baz) is false
    isset($baz) is false

    if($quux) is false
    isset($quux) is true
    if(strcmp(, "") == 0) is true

    if($quuux) is false
    isset($quuux) is true

    if($quuuux) is false
    isset($quuuux) is true

    if($foobar[1]) is false
    isset($foobar[1]) is false

  10. #10
    SitePoint Addict sstaubin01's Avatar
    Join Date
    Nov 2000
    Location
    Ont, Canada
    Posts
    234
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Freakysid. That puts it in perspective. It seems that an empty form field is actually an empty string therefore isset returns a true value.

    Bottom line. if (isset($var)) has it's own function while
    if ($var) has another, although they are similar. Truly understanding these concepts is necessary or you may not get expected results. I don't think using isset as a rule of thumb is a good idea.

    Any other thoughts?


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
  •