如何将php变量传递给.php包含?

I have a file, lets say it's index.php where the very beginning of the file has an include for "include.php". In include.php I set a variable like this:

<?php $variable = "the value"; ?>

then further down the in index.php I have another include, say "include2.php" that is included like this:

<?php include(get_template_directory_uri() . '/include2.php'); ?>

How can I call the "$variable" that I set in the first include, in "include2.php"?

The exact code that I am using is as follows:

The very first line of the index.php I have this line

<?php include('switcher.php'); ?>

Inside switcher.php I have this

<?php $GLOBALS["demo_color"] = "#fffffe"; ?>

If I use this in index.php, it works

<?php echo $GLOBALS["demo_color"]; ?>

However, If I use the following code to include another php file

<?php include(get_template_directory_uri() . '/demo_color.php'); ?>

then inside demo_color.php I have this code:

<?php echo "demo color:" . $GLOBALS["demo_color"]; ?>

The only thing it outputs is "demo color:"

edited for code-formatting

It simply can be used in include2.php, unless the inclusion of include.php happens inside of a different scope (i.e. inside a function call). see here.

If you want to be completely explicit about the intention of using the variable across the app, use the $GLOBALS["variable"] version of it's name, which will always point to the variable called variable in the global scope.

EDIT: I conducted a test against php 5.3.10 to reconstruct this:

// index.php
<?php
include("define.php");
include("use.php");

// define.php
$foo = "bar";

// use.php
var_dump($foo);

This works exactly as expected, outputting string(3) "bar".

<?PHP
//index.php
$txt='hello world';
include('include.php');

<?PHP
//include.php
echo $txt; //will output hello world

So it does work. Though there seems to be a bigger issue since this is likely to be difficult to maintain in the future. Just putting a variable into global namespace and using it in different files is not a best practice.

To make the code more maintainable it might be an idea to use classes so you can attach the variables you need explicit instead of just using them. Because the code around is not showed it is not clear what is your exact need further but it will be likely the code can be put in classes, functions etc. If it is a template you could think about an explicit set() function to send the variable data to the templates and extract() it there.

edit:

In addition based on the information first set your error_reporting to E_ALL and set the display_errors to 1. So you get all errors since the information you placed in your updated question gives indications that a missing variable is used as a constant which should raise errors all over the place.