Page 1 of 1

isset and != ''

Posted: Sun Feb 20, 2005 3:29 pm
by jonemo
is there any difference between the following two code snippets?

Code: Select all

if (isset($t))

Code: Select all

if ($t != '')
i.e. can it happen that the two conditions have different results with the same $t?

Posted: Sun Feb 20, 2005 3:33 pm
by feyd
the second can, and will produce an "undefined variable: t" error/warning/notice. The first will not.

Posted: Sun Feb 20, 2005 3:42 pm
by John Cartwright
you're better off using

empty() and isset()


feyd | ;)

Posted: Sun Feb 20, 2005 4:02 pm
by timvw
with array keys there are some differences between != '', isset, array_key_existst and empty.

Posted: Sun Feb 20, 2005 4:06 pm
by jonemo
so what is the difference between between empty() and isset() then? and is it only for arrays or is a variable that gets the value NULL still set? as the main use for stuff like that is to check $_REQUEST[] stuff, it deals with arrays...

Posted: Sun Feb 20, 2005 4:13 pm
by timvw
how about reading the manual and experimenting yourself?

Code: Select all

<?php
echo "<br>nothing set<br>";

if (isset($val))
&#123;
    echo "isset";
&#125;
else
&#123;
    echo "not isset";
&#125;
echo "<br>";
if (empty($val))
&#123;
    echo "empty";
&#125;
else
&#123;
    echo "not empty";
&#125;

echo "<br> assign test<br>";

$val = "test";
if (isset($val))
&#123;
    echo "isset";
&#125;
else
&#123;
    echo "not isset";
&#125;
echo "<br>";
if (empty($val))
&#123;
    echo "empty";
&#125;
else
&#123;
    echo "not empty";
&#125;

echo "<br>assign null<br>";

$val = null;
if (isset($val))
&#123;
    echo "isset";
&#125;
else
&#123;
    echo "not isset";
&#125;
echo "<br>";
if (empty($val))
&#123;
    echo "empty";
&#125;
else
&#123;
    echo "not empty";
&#125;
echo "<br>";
?>

feyd | naughty timvw! ;)

Posted: Sun Feb 20, 2005 5:31 pm
by jonemo
why not ask the clever people in the forum before writing test scripts?

thanks for the code, but i actually just coded the same (before i saw your answer) :)