Re: included file var scope

From: Date: Mon, 07 Apr 2008 14:46:57 +0000
Subject: Re: included file var scope
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-272636@lists.php.net to get a copy of this message
Evert Lammerts wrote:
My system is accessible through an index.php file. This file does a bunch of includes to include global variables, which in function context become available through the global keyword: index.php <?php require_once "global_vars.php"; function f() { global $global_var; var_dump($global_var); } f(); ?> However, apart from access through the index.php file I want to be able to include my index.php file from a function context: <?php function includeIndex() {
      require_once ("index.php");
} ?> The logical result is that variables that are globally accessible to functions when the system is accessed through index.php are now in the context of includeIndex() - so they're not global. I want to fix this problem by not having the include inside of the function context. How to go upon this?
In index.php rather than declaring vars like so... $var = 'value'; ...declare them in the $GLOBALS array like so... $GLOBALS['var'] = 'value'; $var is then in the global scope regardless of where it was set. -Stut -- http://stut.net/

« previous php.general (#272636) next »