note 35426 added to functions

From: Date: Sun, 31 Aug 2003 20:59:29 +0000
Subject: note 35426 added to functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-55385@lists.php.net to get a copy of this message
Hi, it seems to me the keyword 'global' not only imports existing global variables to the scope of a function but even ensures the variables exists in any case, which means if the global variable does not exists it is created. I thus wrote two functions: function needGETVar($idx) { global ${"GET".$idx}; if(isset($_GET[$idx])) ${"GET".$idx}=$_GET[$idx]; return; } function needPOSTVar($idx) { global ${"POST".$idx}; if(isset($_POST[$idx])) ${"POST".$idx}=$_POST[$idx]; return; } I pass the index to either of the functions of data I expect to be POSTed or GET'ed (GOT :-) by my script. After calling these functions like: needGETvar('foo'); a Variable $GETfoo exists. Either with the value passed in the URL or empty, but it exists. That means I can compare any value with the data I expect to be passed in the URL without having to check with isset($_GET['foo']) if it really was passed. Bernhard ---- Manual Page -- http://www.php.net/manual/en/functions.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35426 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35426&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35426&report=yes

« previous php.notes (#55385) next »