Search code examples
phpincludezend-studiozend-debugger

PHP / Zend complains about undefined variables


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.