note 69950 modified in function.isset by thiago
| From: | thiago@php.net | Date: | Tue, 05 Aug 2008 18:42:45 +0000 |
| Subject: | note 69950 modified in function.isset by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143177@lists.php.net to get a copy of this message | ||
The unexpected results of isset has been really frustrating to me. Hence, it doesn't work how
you'd think it would, (as documented) a var currently in the scope with a null value will
return false.
Heres a quick solution, perhaps there are better ways of going about this, but heres my solution...
<?php
function is_set( $varname, $parent=null ) {
if ( !is_array( $parent ) && !is_object($parent) ) {
$parent = $GLOBALS;
}
return array_key_exists( $varname, $parent );
}
?>
Hence, $varname should be a mixed value of var's to check for, and $parent can be an array or
object, which will default to the GLOBAL scope. See the documentation of array_key_exists for
further information.
This will allow to check if a var is in the current scope, object, or array... Whether it's a
null, false, true, or any value. It depends on ARRAY_KEY_EXISTS for it's functionality which
also works with Objects. Feel free to improve on this anyone ;D
--was--
The unexpected results of isset has been really frustrating to me. Hence, it doesn't work how
you'd think it would, (as documented) a var currently in the scope with a null value will
return false.
Heres a quick solution, perhaps there are better ways of going about this, but heres my solution...
function is_set( $varname, $parent=null ) {
if ( !is_array( $parent ) && !is_object($parent) ) {
$parent = $GLOBALS;
}
return array_key_exists( $varname, $parent );
}
Hence, $varname should be a mixed value of var's to check for, and $parent can be an array or
object, which will default to the GLOBAL scope. See the documentation of array_key_exists for
further information.
This will allow to check if a var is in the current scope, object, or array... Whether it's a
null, false, true, or any value. It depends on ARRAY_KEY_EXISTS for it's functionality which
also works with Objects. Feel free to improve on this anyone ;D
http://php.net/manual/en/function.isset.php