note 69950 added to function.isset
| From: | randallgirard at hotmail dot com | Date: | Wed, 27 Sep 2006 18:51:04 +0000 |
| Subject: | note 69950 added to function.isset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-117743@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...
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
----
Server IP: 64.71.164.2
Probable Submitter: 71.119.235.161
----
Manual Page -- http://www.php.net/manual/en/function.isset.php
Edit -- https://master.php.net/note/edit/69950
Del: integrated -- https://master.php.net/note/delete/69950/integrated
Del: useless -- https://master.php.net/note/delete/69950/useless
Del: bad code -- https://master.php.net/note/delete/69950/bad+code
Del: spam -- https://master.php.net/note/delete/69950/spam
Del: non-english -- https://master.php.net/note/delete/69950/non-english
Del: in docs -- https://master.php.net/note/delete/69950/in+docs
Del: other reasons-- https://master.php.net/note/delete/69950
Reject -- https://master.php.net/note/reject/69950
Search -- https://master.php.net/manage/user-notes.php