Forum Moderators: coopster

Message Too Old, No Replies

Var in required file can't bee seen in Index

         

neophyte

1:03 am on Jan 11, 2010 (gmt 0)

10+ Year Member



Hello All -

I don't understand why this is happening:

I've got a regular index.php file which requires a number of other .php files via a loop in a function.

These other required .php files all have various vars and arrays for use farther on down in the index.php file.

The strange part is, while I can confirm that these additional files ARE being included (required) the various variables/arrays which they initialize cannot be seen by index after they're required.

More clearly:

1. index is fired
2. index requires some_file.php (via a loop within a function). Within some_file.php is a line of code that says $foo = 'Something';
3. After some_file.php is required, index tries to use $foo but I get the error: - Undefined variable: foo in E:\WEBDEV\Directory\index.php on line 20.

Could this because the file being required is done so through a loop in the function? As such, are the vars within the required files being caught in the scope of the function and not passed back to index?

Any help greatly appreciated!

Neophyte

rocknbil

3:26 am on Jan 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



(via a loop within a function).

And there it is. :-)

ADDED NOTE: I re-read the problem, and may have it backwards. you want the variables INSIDE the function to become global, used outside the function, correct? If this is the case, you would have to declare those as global (or, see note at bottom about returning values from function, which would then be global.) Never done it that way, but it will probably work.

Or you could just write a class for your function . . . my backwards-example follows.

$foo = 'Boo';
some_function();
function some_function() {
echo $foo;
}

undefined variable "foo" on line 4

$foo = 'Boo';
some_function();
function some_function() {
global $foo;
echo $foo;
}

--> Boo

However, globals is highly discouraged in PHP. I suggest passing external function variables as parameters:

$foo = 'Boo';
some_function($foo);
function some_function($bar) {
echo $bar;
}

--> Boo

An additional thought, taking into account the added note above, why can't you do something like this when calling the function?

list($foo,$bar) = some_function();
function some_function() {
$avar='1';
$bvar='2';
$r = Array($avar,$bvar);
return $r;
}

neophyte

10:42 am on Jan 11, 2010 (gmt 0)

10+ Year Member



RocknBill -

Thanks for you insight and all of your examples. After thinking on it some more and re-reading your reply, I tried declaring $foo as a global at the top of my "require_once" file before initializing it with a value.

Now, the file get's included (required) as it's suppose to via a loop in a called function, and $foo is available to my index file further on down the script as needed.

Are globals "highly discouraged" depending on the type of information they contain (like passwords and other sensitive information) or just as a general matter of course?

rocknbil

10:49 pm on Jan 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I **think** you would want to declare it as global within the function, because it's automatically a global if it springs to life outside the function. That's just semantics though, if it works, it works.

Regarding not using globals: truth, I don't know. Most of the arguments against using global variables have more to do with compartmentalizing your code, making it portable, than anything else. That is, you write a function and declare a glob inside it:

function some_func() {
global $some_var;
}

Now if you move that function to another application, it pretty much relies on the global $some_var being created somewhere else in the program (otherwise why would you declare it as a global in the first place.)

So I don't think it's an issue of security, it's an issue of coding style. Don't confuse $_GLOBALS or REGISTER_GLOBALS, which does have a lot of documentation about security concerns, with declaring a variable as global - these are two different topics.

neophyte

10:04 am on Jan 15, 2010 (gmt 0)

10+ Year Member



rocknbil -

Thanks for the explanation and insight!

Neophyte