tags:

views:

246

answers:

4

If I have $var defined in Page1.php and in Page2.php I have

//Page2.php
include('Page1.php');

echo $var;

For what reasons will it not print the value of $var to the screen? The files are in the same directory so paths shouldn't be the issue. I've checked the php.ini file and nothing really jumps out at me. Any ideas?

Thanks

+2  A: 

Possible causes:

  • The current working path isn't always the same as the file's. For example, if Page2.php is being included at a higher level, that higher level will be the path. Either make sure you've loaded Page2.php directly or move Page1.php accordingly.
  • Make sure $var is really what you expect it to be. Echo it in Page1.php to confirm. (this also checks the right file is being included)
  • If the source isn't really this simple, make sure you're not undefining/clearing $var anywhere.
Oli
A: 

I've just tested:

page1.php:

<?php
$foo = "bar"
?>

page2.php:

<?php
include('page1.php');
echo $foo;
?>

This works.

You might want to check your file paths. Also var_dump() is handy to check the variable output.

andyuk
A: 

If it were a path problem you would see a warning in your error log. You could also change to require instead of include and it would become obvious.

echo getcwd();

You can also print your working directory to figure out what's wrong.

Is $var created in a function? If so, make sure you have

global $var;

before the first assignment in that function.

Mnebuerquo
A: 

Check that you not turned off the whole error_reporting. Maybe the include goes wrong and so the variable never is included.

Markus Lux