It seems like my code works to check for null if I do
if ($tx)
or
if (isset($tx))
why would I do the se
Because the first produces a warning, and will also return true if $tx === false, which is not the same as not defined
It also important to note that a string in PHP could be set but empty. So the isset would return true even though there is nothing in the string, to prevent this I use a function something like this to replace isset:
<?php
function exists($var) {
if (isset($var) && $var != "") {
return true;
} else {
return false;
}
}
?>
isset() has nothing to do with TYPE or VALUE - only with EXISTENCE.
if ($condition)... will evaluate the VALUE OF THE VARIABLE as a boolean value.
if ( isset($condition) )... will evaluate the EXISTENCE OF THE VARIABLE VALUE as boolean.
isset() can be false for two reasons.
Firstly, because the variable is not set, and so has no value.
Secondly, because a variable is NULL, which means "unknown value" and can't be considered set because it includes "no value" and because so many people use $v = null to mean the same thing as unset($v).
(Remember, if you specifically want to check for null, use is_null().)
isset() is usually used to check an external variable that may or may not exist.
For example, if you have a page called page.php, that has this:
ini_set('display_errors', 1);
error_reporting(E_ALL);
if ( $_GET["val"] ) {
// Do Something
} else {
// Do Nothing
}
it will work ok for any of these URLS:
http://www.example.com/page.php?val=true // Something will be done.
http://www.example.com/page.php?val=monkey // Something will be done.
http://www.example.com/page.php?val=false // Nothing will be done.
http://www.example.com/page.php?val=0// Nothing will be done.
However, you will receive an error for this URL:
http://www.example.com/page.php
because there is no 'val' argument in the URL, so there is no 'val' index in the $_GET array.
The correct way to do this is like:
if ( isset($_GET["val"]) ) {
if ( $_GET["val"] ) {
// Do Something
} else {
// Do Nothing
}
} else {
// $_GET["value"] variable doesn't exist. It is neither true, nor false, nor null (unknown value), but would cause an error if evaluated as boolean.
}
Though there are shortcuts for this.
You can check for a combination of existence and certain Boolean conditions with empty(),
if ( !empty($_GET["val"]) ) {
// Do someting if the val is both set and not empty
// See http://php.net/empty for details on what is considered empty
// Note that null is considered empty.
}
or
if ( isset($_GET["val"]) and $_GET["val"] ) {
// Do something if $_GET is set and evaluates to true.
// See php.net logical operators page for precedence details,
// but the second conditional will never be checked (and therefor
// cause no error) if the isset returns false.
}