note 30345 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:41:24 +0000 |
| Subject: | note 30345 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155576@lists.php.net to get a copy of this message | ||
Note Submitter: j at superjonas dot de
----
> 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"
}
?>