Jump to content

Weird problem: Included files do not register variables declared in the parent?


hatrickpatrick

Recommended Posts

I have a weird problem with a script I'm writing:

 

If I have a file, file1.php, and a second file, file2.php:

file1.php contains this:

<?
$text="The Text String";
?>

file2.php is this:

<?
include("file1.php");
echo $text;
?>

 

file2.php will output "The Text String".

 

However, the following scripts will NOT output anything:

file1.php:

<?
$text="The Text String";
include("file2.php");
?>

 

file2.php:

<?
echo $text;
?>

 

Why is this? It seems that after you've included a file, the parent file can call variables from the included file, but an included file cannot use variables declared in the parent? Hve I misconfigured php.ini or something?

The code you posted works. There is no setting that changes how parent/child included code works.

 

It is unlikely that the posted code is your actual code. Your actual code either has a syntax error that is preventing it from running, an incorrect file name in the include statement, or some other problem or error that is preventing it from working.

 

To get the quickest solution to what your code is doing, you need to post the actual code that is generating the symptoms you are seeing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.