Re: included file var scope

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux