Save this as include-me.inc.php
<?php
// just defines - I'll be whevever my scope is
$isItGlobal1 = "I'm global 1, nice to meet you";
// I'm global - but if you're a function can you just use me?
$GLOBALS['isItGlobal2'] = "I'm global 2, how do you like them apples";
// uses global array, then declares global
$GLOBALS['isItGlobal3'] = "Global 3, dancing a jig... guess what I'm doing next?";
global $isItGlobal3;
// declares global before it's even defined
global $isItGlobal4;
$isItGlobal4 = 'Global 4, cook my dinner';
// mystery man
$isItGlobal5 = 'Global 5 - dancing';
?>
And this as whatever
<?php
include_it();
echo __FUNCTION__.'Global #1: '.$isItGlobal1.'<br />';
echo __FUNCTION__.'Global #2: '.$isItGlobal2.'<br />';
echo __FUNCTION__.'Global #3: '.$isItGlobal3.'<br />';
echo __FUNCTION__.'Global #4: '.$isItGlobal4.'<br />';
echo __FUNCTION__.'Global #5: '.$isItGlobal5.'<br />';
echo '<br />';
hello_globals();
/* includes the file */
function include_it()
{
require_once('include-me.inc.php');
global $isItGlobal5; // hmmm, ok - declared global after, remove this line later and run again
echo 'Live from include_it we have the following guests:<br />';
echo __FUNCTION__.'() Global #1: '.$isItGlobal1.'<br />'; // fine
echo __FUNCTION__.'() Global #2: '.$isItGlobal2.'<br />'; // NOT THERE!
echo __FUNCTION__.'() Global #3: '.$isItGlobal3.'<br />'; // fine
echo __FUNCTION__.'() Global #4: '.$isItGlobal4.'<br />'; // fine
echo __FUNCTION__.'() Global #5: '.$isItGlobal5.'<br />'; // eh?
echo '<br />include_it signing off...<br /><br />';
}
function hello_globals()
{
global $isItGlobal1, $isItGlobal2, $isItGlobal3, $isItGlobal4, $isItGlobal5;
echo __FUNCTION__.'() here, live with the following guests:<br />';
echo __FUNCTION__.'() Global #1: '.$isItGlobal1.'<br />';
echo __FUNCTION__.'() Global #2: '.$isItGlobal2.'<br />';
echo __FUNCTION__.'() Global #3: '.$isItGlobal3.'<br />';
echo __FUNCTION__.'() Global #4: '.$isItGlobal4.'<br />';
echo __FUNCTION__.'() Global #5: '.$isItGlobal5.'<br />';
echo '<br />';
}
?>
See if you understand what's going on, and how it should affect your include scripts.