note 30345 added to language.variables.scope

From: Date: Fri, 14 Mar 2003 16:08:51 +0000
Subject: note 30345 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-45314@lists.php.net to get a copy of this message
> pim wrote: > in addition: > if you define a function in that included file, it can't get > the variables from the inluded file's scope. global won't work. > The only way to give such an include function access to global > vars is via arguments. I don't know if this is a bug in PHP. > > //---- from within function included file ----- > echo $var1; // this one works > function foo() > { > global $var1; > echo $var1; // this one doesn't > } It works if you additionally declare the variables from the inluded file's scope as global. example: <?php /* file1.php */ function func1() { include("file2.php"); func2(); } func1(); ?> <?php /* file2.php */ global $var; // declare as global here $var = 'something'; function func2() { global $var; // again here echo $var; // prints "something" } ?> -- http://www.php.net/manual/en/language.variables.scope.php http://master.php.net/manage/user-notes.php?action=edit+30345 http://master.php.net/manage/user-notes.php?action=delete+30345 http://master.php.net/manage/user-notes.php?action=reject+30345

« previous php.notes (#45314) next »