note 80790 added to function.array-key-exists
| From: | packard_bell_nec at hotmail dot com | Date: | Thu, 31 Jan 2008 17:46:09 +0000 |
| Subject: | note 80790 added to function.array-key-exists | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135670@lists.php.net to get a copy of this message | ||
You can check whether a variable is defined by using array_key_exists()!
First, you may ask that no reserved array (would be called $LOCALS) is predefined in function scope
(contrast to reserved array $GLOBALS in global scope. To solve it, you can use compact().
Then, you may ask that why property_exists() cannot be used. This is because no reserved function is
predefined to create OBJECT containing variables and their values, and no reserved function is
predefined to import variables into the current symbol table from an OBJECT. In addition,
property_exists() breaks the naming convention of reserved function.
Finally, I show how to check whether a variable is defined by using array_key_exists():
<?php
function too(){
$roo = array_key_exists('foo', compact('foo'));
echo ($roo?'1':'0').'<br/>';
$foo = null;
$roo = array_key_exists('foo', compact('foo'));
echo ($roo?'1':'0').'<br/>';
}
too();
?>
The output will be:
0<br/>
1<br/>
----
Server IP: 58.65.236.81
Probable Submitter: 124.244.59.57
----
Manual Page -- http://www.php.net/manual/en/function.array-key-exists.php
Edit -- https://master.php.net/note/edit/80790
Del: integrated -- https://master.php.net/note/delete/80790/integrated
Del: useless -- https://master.php.net/note/delete/80790/useless
Del: bad code -- https://master.php.net/note/delete/80790/bad+code
Del: spam -- https://master.php.net/note/delete/80790/spam
Del: non-english -- https://master.php.net/note/delete/80790/non-english
Del: in docs -- https://master.php.net/note/delete/80790/in+docs
Del: other reasons-- https://master.php.net/note/delete/80790
Reject -- https://master.php.net/note/reject/80790
Search -- https://master.php.net/manage/user-notes.php