Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.