note 80791 deleted from function.compact by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 12:17:55 +0000 |
| Subject: | note 80791 deleted from function.compact by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197155@lists.php.net to get a copy of this message | ||
Note Submitter: packard_bell_nec at hotmail dot com
----
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/>