<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>References all variables available in global scope</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.variables.superglobals.html">Superglobals</a></div> <div class="next" style="text-align: right; float: right;"><a href="reserved.variables.server.html">$_SERVER</a></div> <div class="up"><a href="reserved.variables.html">Predefined Variables</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="reserved.variables.globals" class="refentry"> <div class="refnamediv"> <h1 class="refname">$GLOBALS</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">$GLOBALS</span> — <span class="dc-title">References all variables available in global scope</span></p> </div> <div class="refsect1 description" id="refsect1-reserved.variables.globals-description"> <h3 class="title">Description</h3> <p class="para"> An associative <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 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> <div class="refsect1 examples" id="refsect1-reserved.variables.globals-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="variable.globals.basic"> <p><strong>Example #1 <var class="varname"><var class="varname">$GLOBALS</var></var> example</strong></p> <div class="example-contents"> <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"><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> </div> <div class="refsect1 notes" id="refsect1-reserved.variables.globals-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para">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. </p></p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <strong>Variable availability</strong><br /> <p class="para"> Unlike all of the other <a href="language.variables.superglobals.html" class="link">superglobals</a>, <var class="varname"><var class="varname">$GLOBALS</var></var> has essentially always been available in PHP. </p> </p></blockquote> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.variables.superglobals.html">Superglobals</a></div> <div class="next" style="text-align: right; float: right;"><a href="reserved.variables.server.html">$_SERVER</a></div> <div class="up"><a href="reserved.variables.html">Predefined Variables</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>