Which function should I use for testing if a var is set or not?
I'm sometimes confused to using which one of 开发者_如何学Cthem,
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 ===''
?
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.
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 ==
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
精彩评论