12 comments
Mazzu Says:
To check if the variable doesn't exist and to not be confused with null, you may use :
- for key of array : array_key_exists($key, $array)
- for property of object : property_exists($property)
- for global variable : array_key_exists('variable', $GLOBALS)
- for local variable : I don't know
Janci Says:
Re Mazzu: for a local variable you could test against the array returned by get_defined_vars() function like this: array_key_exists('foo', get_defined_vars());
get_defined_vars() returns a multi-dimensional array containing all defined variables within the scope. Note that if called on global level (i.e. outside a function/method), it will also return all global variables. More info: http://uk.php.net/manual/en/function.get-defined-vars.php
Sjon Says:
The main difference between an unset variable and a null variable is that the unset variable doesn't exist in the current scope, whereas the null variable is a defined variable with no value (because null is essentially the i-have-no-value value).
The isset() function is in that way confusing because it not only checks whether the variable is defined in the current scope, but also if it has a value which is not null.
The is_null() function is indeed the best way to determine if a variable has a null value, or, if you must, the === operator. The empty() function is not a good way, because it will also return true for empty strings and a zero.
Regards,
Janci Says:
Sjno, unfortunatelly, there's not much difference between using isset() and is_null() as these two functions are reverse (i.e. if isset() returns true, is_null() returns false and vice versa). The only difference is that if passing non-existing/unset variable to is_null(), it generates a notice (but still returns true).
As I said in my previous post, it seems that the only way to find out whether a variable is actually set (even if null) is to use the get_defined_vars().
Janci Says:
Sjon, sorry for misspelling your nick, dude.
Sjon Says:
@Janci,
I know it may look that way, but they are not really each other's opposites. I think empty() would be a better fit for isset(). Both those functions can be used to check for a/no value, whereas the is_*() family is used to check for a certain type of variable.
Using get_defined_vars() may be the best way to determine if a variable is defined, but because of the array search also slow(er). Besides, if you really need to check if a variable is set, even if null, I think you're looking at a weird/wrong solution to your problem (at least, I can't think of any good reason why. If you've got a good example, please share).
A good example of how to use null values and is_null is something like this (in quickly mocked up pseudocode):
<?php
$name = $email = null;
if (isset($_POST['name']) && isValid($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'] && isValid($_POST['email'])) {
$email = $_POST['email'];
}
if (is_null($name) || is_null($email)) {
displayMissingRequiredInput();
}
else {
processInput();
}
?>
Oh, and about the misspelling, no worries... :-)
Regards,
Sjon Says:
(rest of my post)
$name = $email = null;
if (isset($_POST['name']) && isValid($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'] && isValid($_POST['email'])) {
$email = $_POST['email'];
}
if (is_null($name) || is_null($email)) {
displayMissingRequiredInput();
}
else {
processInput();
}
Oh, and about the misspelling, no worries... :-)
Regards,
Janci Says:
@Sjon: I do completely agree with all you've said. From the practical point of view, using get_defined_vars() means somethings rotten in your code. I was just trying to point out the tricks&catches when working with nulls in PHP.
Winer Reales Says:
Sure is best to use
if (is_null($name) || is_null($email))
in PHP5 you can't use isset() for variable only for http request, post or get
Comments are disabled for this post.



Juan Juarez Says:
Keeping track of the different comparison operators can be confusing. I often find myself referring to this chart : http://www.php.net/manual/en/types.comparisons.php