note 80791 added to function.compact

From: Date: Thu, 31 Jan 2008 17:46:51 +0000
Subject: note 80791 added to function.compact
Groups: php.notes 
Request: Send a blank email to php-notes+get-135671@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: 69.147.83.197 Probable Submitter: 124.244.59.57 ---- Manual Page -- http://www.php.net/manual/en/function.compact.php Edit -- https://master.php.net/note/edit/80791 Del: integrated -- https://master.php.net/note/delete/80791/integrated Del: useless -- https://master.php.net/note/delete/80791/useless Del: bad code -- https://master.php.net/note/delete/80791/bad+code Del: spam -- https://master.php.net/note/delete/80791/spam Del: non-english -- https://master.php.net/note/delete/80791/non-english Del: in docs -- https://master.php.net/note/delete/80791/in+docs Del: other reasons-- https://master.php.net/note/delete/80791 Reject -- https://master.php.net/note/reject/80791 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135671) next »