note 43794 added to functions

From: Date: Sun, 04 Jul 2004 01:33:37 +0000
Subject: note 43794 added to functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-72496@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/functions.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43794 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43794&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43794&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#72496) next »