PHP / Zend complains about undefined variables
-
13-09-2019 - |
Question
The situation:
index.php:
<?php
include("foo.php");
include("baz.php");
foo("bar.php");
?>
baz.php:
<?php
$x = 42;
?>
foo.php:
<?php
function foo($p) {
include_once($p); // please dont mind the inclusion hole
}
?>
bar.php:
<?php
echo $x;
?>
Zend notice: Undefined variable: x
Placing global $x; in bar.php removes the notice, but I understand why there is a notice about this in the first place.. Doesn't include pretty much work like including C headers? It would mean that the interpreted code would look like this:
<?php
function foo($p) {
include_once($p); // please dont mind the inclusion hole
}
$x = 42;
// this however, is included by a function...
// does the function's scope influence the stuff it includes?
echo $x; // undefined variable
?>
My editor is the Eclipse/Zend package.
Solution
I'm no expert, so please don't flame me if I'm wrong, but I think the file called by include_once or require_once is called in the context of the caller. Since function foo() won't know about $x then neither will any of its called includes. You could experiment by 'declaring' $x inside function foo() with the same setup as above.
OTHER TIPS
I get a bunch of those notices since I'm almost allways goes with "$o .= 'foo'" without any definition. I'm just hiding them with error_reporting(E_ALL ^ E_NOTICE), but I don't know if it's the optimal way in this case.
It doesn't work even if the variable and the function are in the same file.
1 <?php
2
3 $x = 3;
4
5 function t()
6 {
7 echo $x;
8 }
9
10 t();
Prints nothing.
But add a global
1 <?php
2
3 $x = 3;
4
5 function t()
6 {
7 global $x;
8 echo $x;
9 }
10
11 t();
and you can see "3".
In a function you can't see global variables unless you declare it.
yes its the function scope that is causing your issues
if you replace
foo("bar.php");
with
include("bar.php");
you'll see that everything works fine because it puts it into the current scope and not functions scope