note 43794 deleted from functions by tomsommer

From: Date: Sun, 04 Jul 2004 01:46:12 +0000
Subject: note 43794 deleted from functions by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72497@lists.php.net to get a copy of this message
Note Submitter: federico at datashare dot com dot ar ---- An include() call within a function brings the problem of not being able to reach the included variables from the main document or else having to declare all the vars in the included file as global. The following won't work (in fact it hides $GLOBALS) <? function myFunction(){ global $GLOBALS; ... } ?> The following works but you get o copy of the variables in the global scope, not a reference to them, so changing values of vars in the function won't affect the var in the global scope. <? function myFunction(){ extract($GLOBALS); ... } ?> This will work fine <? function myIncludeFunction($f){ while(list($key, $val)=each($GLOBALS)){ // very important: omitting GLOBALS var if($key != "GLOBALS") global $$key; // double dollar refers to a variable variable } ... // in my case i needed this to solve the problem // of including within a function include($f); ... } ?> Thus, when calling myIncludeFunction() it acts as if the include was performed within the main document. It's a shame you can't just put the while block in a makeAllGlobal() function and then just call it from other functions. That won't work. HTH Fede

« previous php.notes (#72497) next »