0

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.

Charles
  • 50,943
  • 13
  • 104
  • 142

4 Answers4

3

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.

Dave Archer
  • 3,022
  • 20
  • 23
  • I am lead to believe you're right. Placing a global $x; inside the foo() function does also fix the notices error. I would be interested to see any doc page about this, and why PHP still manages to execute the code just fine but produces a notice. –  Jun 15 '09 at 09:08
  • 1
    The reason why you don't get an error is simple: Using undeclared variables is not an error in PHP. It always produces a warning and continues execution. That's why I always prefer declaring my own error handlers and throwing an exception on all errors/warnings/notices. Good code should not produce any of them anyway. – soulmerge Jun 15 '09 at 09:16
0

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.

Ivar
  • 4,344
  • 6
  • 38
  • 53
0

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.

Dutow
  • 5,638
  • 1
  • 30
  • 40
0

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

Galen
  • 29,976
  • 9
  • 71
  • 89