Your code will work, but you're not quite using the right metaphor here.
When a script includes another file, imagine that you have opened both files in a text editor, copied the code from the included file, and pasted that code into the script that invoked include().
The only difference between cutting-and-pasting and include() is that when the included file is run, PHP switches back to HTML throughput mode. That's why you have to wrap the included file's code in "<?php...?>" tags.
The code you've inserted uses the variable scope of the including file as the variable scope exists that that point in your code.
Three examples, both will include the file "toinclude.inc":
toinclude.inc:
script1.php:
Code:
<?php
$a = 3;
include ('toinclude.inc');
?>
When you invoke script1.php, the output returned will be "3". $a exists in the global scope and is available to the code in toinclude.inc.
script2.php:
Code:
<?php
function foo()
{
include ('toinclude.inc');
}
$a = 3;
foo();
?>
If you point your browser to script2.php, you will get no output. The included file inherits the variable scope of the inside of the function, and $a is not available. PHP will create a new $a that exists only inside the function and initialize that function to a null value.
script3.php:
Code:
<?php
function foo()
{
global $a;
include ('toinclude.inc');
}
$a = 3;
foo();
?>
This one will output "3", also. Although the included file inherits the variable scope of the function foo, that function now imports $a from the global variable scope to the local function variable scope. Thus, the value will be available to the included file.
This is the reason I generally recommend using the superglobal arrays ($_POST, $_GET, $_SERVER, etc.) whenever possible. These variables are available anywhere because by definition they are a part of every variable scope.
Want the best answers? Ask the best questions: TANSTAAFL!!