Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which function should I use for testing if a var is set or not?

Tags:

php

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;
}

}

That's sometimes not clear to me, sometimes 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 ? empty ? or ===''?

like image 369
streetparade Avatar asked Mar 19 '10 15:03

streetparade


2 Answers

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";

$emptyString = '';

echo 'is_null($emptyString) = ' . (is_null($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($emptyString) = ' . (empty($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($emptyString) = ' . (isset($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$emptyString = ' . ($emptyString ? '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($emptyString) = FALSE
empty($emptyString) = TRUE
isset($emptyString) = TRUE
(bool)$emptyString = 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.

like image 180
ryeguy Avatar answered Oct 17 '22 10:10

ryeguy


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 ==

like image 24
brettkelly Avatar answered Oct 17 '22 10:10

brettkelly