Source of: /manual/en/reserved.variables.globals.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/reserved.variables.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'reserved.variables.globals.php',
1 => '$GLOBALS',
),
'up' =>
array (
0 => 'reserved.variables.php',
1 => 'Predefined Variables',
),
'prev' =>
array (
0 => 'language.variables.superglobals.php',
1 => 'Superglobals',
),
'next' =>
array (
0 => 'reserved.variables.server.php',
1 => '$_SERVER',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="reserved.variables.globals" class="refentry">
<div class="refnamediv">
<h1 class="refname">$GLOBALS</h1>
<p class="refpurpose"><span class="refname">$GLOBALS</span> — <span class="dc-title">References all variables available in global scope</span></p>
</div>
<a name="reserved.variables.globals.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<p class="para">
An associative <a href="language.types.array.php" class="type array">array</a> containing references to all variables which
are currently defined in the global scope of the script. The
variable names are the keys of the array.
</p>
</div>
<a name="reserved.variables.globals.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 <var class="varname">$GLOBALS</var> example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"local variable"</span><span style="color: #007700">;<br /><br /> echo </span><span style="color: #DD0000">'$foo in global scope: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$GLOBALS</span><span style="color: #007700">[</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">'$foo in current scope: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$foo </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"Example content"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output
something similar to:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
$foo in global scope: Example content
$foo in current scope: local variable
</pre></div>
</div>
</div><p>
</p>
</div>
<a name="reserved.variables.globals.notes"></a><div class="refsect1 notes">
<h3 class="title">Notes</h3>
<blockquote><p><b class="note">Note</b>: This is a 'superglobal', or
automatic global, variable. This simply means that it is available in
all scopes throughout a script. There is no need to do
<strong class="command">global $variable;</strong> to access it within functions or methods.
<br /></p></blockquote>
<blockquote><p><b class="note">Note</b>:
<b>Variable availability</b><br />
Unlike all of the other <a href="language.variables.superglobals.php" class="link">superglobals</a>,
<var class="varname">$GLOBALS</var> has essentially always been available in PHP.
<br />
</p></blockquote>
</div>
</div><?php manual_footer(); ?>