tags:

views:

101

answers:

3

I'm sometimes confused to using which one of them,

say i have a function called getmember($id)

function getmember($id)
{

// now this is the confusing part 
// how do i test if a $id was set or not set?

//solution 1
if(empty($id))
{
return false;
}


// solution 2

if(isset($id))
{
return false;
}

}

Thats sometimes not clear tome some times if a parameter in a function is set like function($var="")

Then i do

if($var ==="")
{
return false;
} 

What should i use the next time isset ? emtyp ? or ===''

+9  A: 

Here you go, a complete breakdown of what works and when:

<?
echo "<pre>";
$nullVariable = null;

echo 'is_null($nullVariable) = ' . (is_null($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($nullVariable) = ' . (empty($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($nullVariable) = ' . (isset($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$nullVariable = ' . ($nullVariable ? 'TRUE' : 'FALSE') . "\n\n";

$emptyVariable = '';

echo 'is_null($emptyVariable) = ' . (is_null($emptyVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($emptyVariable) = ' . (empty($emptyVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($emptyVariable) = ' . (isset($emptyVariable) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$emptyVariable = ' . ($emptyVariable ? 'TRUE' : 'FALSE') . "\n\n";

//note that the only one that won't throw an error is isset()
echo 'is_null($nonexistantVariable) = ' . (@is_null($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($nonexistantVariable) = ' . (@empty($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($nonexistantVariable) = ' . (isset($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$nonexistantVariable = ' . (@$nonexistantVariable ? 'TRUE' : 'FALSE') . "\n\n";
?>

THE OUTPUT:

is_null($nullVariable) = TRUE
empty($nullVariable) = TRUE
isset($nullVariable) = FALSE
(bool)$nullVariable = FALSE

is_null($emptyVariable) = FALSE
empty($emptyVariable) = TRUE
isset($emptyVariable) = TRUE
(bool)$emptyVariable = FALSE

is_null($nonexistantVariable) = TRUE
empty($nonexistantVariable) = TRUE
isset($nonexistantVariable) = FALSE
(bool)$nonexistantVariable = FALSE

When I show (bool)$variable above, that is how you could use it in a conditional. For example, to check if a variable is null or empty, you could do:

if (!$variable)
    echo "variable is either null or empty!";

But it's best to use a function since it's a little more readable. But it's your choice.

Also, check the PHP type comparison table. It's basically what I just did above, except much more.

ryeguy
comprehensive and accurate, +1
dnagirl
Yepp also a +1 from me thanks for explanation
streetparade
+3  A: 

If you simply want to know if a variable is defined, use isset()

If you want to see if it's been initialized, use is_null()

If you want to compare it's value to something else, use ==

inkedmn
`empty` is better than `is_null()` as sometimes variables are initialised with a zero length string like `$x = '';`
Andy
i think is_null is true in value = null or it was unset before?
streetparade
+1  A: 

Not the same:

isset: Determine if a variable is set and is not NULL http://ch.php.net/manual/en/function.isset.php

empty: Determine whether a variable is empty http://ch.php.net/manual/en/function.empty.php

$a===$b: TRUE if $a is equal to $b, and they are of the same type. http://ch.php.net/manual/en/language.operators.comparison.php

Arko