note 92043 deleted from function.isset by danbrown
| From: | danbrown@php.net | Date: | Mon, 06 Jul 2009 21:46:40 +0000 |
| Subject: | note 92043 deleted from function.isset by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157986@lists.php.net to get a copy of this message | ||
Note Submitter: Daniel Norton
----
The code below shows how to distinguish between an undefined variable and a variable defined as
NULL. I'll keep this code maintained and annotated at http://www.danielnorton.com/nerd/code/php/isset
<?php
global $errhandler_ctr;
function errhandler($errno, $errstr, $errfile, $errline, $errcontext ) {
global $errhandler_ctr;
$errhandler_ctr = $errhandler_ctr ? $errhandler_ctr++ : 1;
return TRUE;
}
function _strval($arg) {
if ((!is_string($arg)) && is_bool($arg)) return $arg ? "TRUE" :
"FALSE";
return strval($arg);
}
$isnull=NULL; // defined as NULL
unset($undef); // not defined
$errhandler_ctr = 0;
$sv = set_error_handler("errhandler");
$is_defined = !is_null($isnull);
set_error_handler($sv);
if (!$errhandler_ctr) $is_defined = TRUE;
print $is_defined ? "TRUE\n" : "FALSE\n";
$errhandler_ctr = 0;
$sv = set_error_handler("errhandler");
$is_defined = !is_null($undef);
set_error_handler($sv);
if (!$errhandler_ctr) $is_defined = TRUE;
print $is_defined ? "TRUE\n" : "FALSE\n";
?>
Output:
TRUE
FALSE