Click to See Complete Forum and Search --> : Globals help


MasterDucky
January 17th, 2011, 03:02 PM
I tried to read about global variables on tutorials but they only talking about globals in user created functions.
So what i'd like to know is if i declare a variable in the main index file will it be global for the files that i include after?
And is it the same for arrays?


...
<?php
$realpath = "G:\\hello.txt";
$titles = array();
include("file1.php");
include("file2.php");
?>
...

PeejAvery
January 17th, 2011, 06:07 PM
You could have tested it out just as easily yourself.

In short...no. Variables from included files will be available in the PHP file calling the included files after the line in which the file is included. But, variables in the main file are not recognized within the included files.

MasterDucky
January 18th, 2011, 05:37 AM
I did test it before asking and was confused because i didnt get any error but still i couldnt change the
values of the variable either.

Now ive declared them in the included files like


$realpath = "G:\\hello.txt";
$GLOBALS["realpath"];
include("switch.php");


but i still cant change them in switch.php (included in the main included file).

Edit: I did a small test and i realize that it works even without the $GLOBALS["realpath"]; so the problem must come from elsewhere.