Re: Scope of include

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

 



On 1/17/07, Andrew Kreps <andrew.kreps@xxxxxxxxx> wrote:
On 1/17/07, jekillen <jekillen@xxxxxxxxxxx> wrote:
> Hello php list:
> If I include a php script inside a php function definition and then
> call the
> function in another script. What is the scope of variables in the
> included
> script? Are they local to the function that calls include with the file
> name?
> Thanks in advance;
> I'm not sure where to look for this answer;
> JK


FYI, I just ran a quick scope test based on my guess of your
situation.  If you include a PHP file containing global variables
inside a function, the variables will _only_ be available after you
include the file, and only for the duration of the function.  They
will not exist after the function ends, even if you specify global
$var1; in your other functions, or at the global level of your script.

This was tested using PHP 5.0.4.

Here is my sample code:

test.php:
<?


print "Top Level: Var1: $var1  Var2: $var2\n<br>";

test1();
test2();

function test1 ()
{
	print "Before: Var1: $var1  Var2: $var2\n<br>";
	include("testinc.php");
	print "After: Var1: $var1  Var2: $var2\n<br>";
}

function test2 ()
{
	print "Test2: Var1: $var1  Var2: $var2\n<br>";
}

print "Bottom Level: Var1: $var1  Var2: $var2\n<br>";


testinc.php:
<?
$var1 = 1;
$var2 = 2;
?>

--
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