<!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>Gets the value of an environment variable</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.get-resources.html">get_resources</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getlastmod.html">getlastmod</a></div> <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.getenv" class="refentry"> <div class="refnamediv"> <h1 class="refname">getenv</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">getenv</span> — <span class="dc-title">Gets the value of an environment variable</span></p> </div> <div class="refsect1 description" id="refsect1-function.getenv-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>getenv</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$varname</code></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$local_only</code><span class="initializer"> = <strong><code>FALSE</code></strong></span></span> ] )</div> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>getenv</strong></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> Gets the value of an environment variable. </p> <p class="para"> You can see a list of all the environmental variables by using <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span>. Many of these variables are listed within <a href="http://www.faqs.org/rfcs/rfc3875" class="link external">» RFC 3875</a>, specifically section 4.1, "Request Meta-Variables". </p> </div> <div class="refsect1 parameters" id="refsect1-function.getenv-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">varname</code></dt> <dd> <p class="para"> The variable name. </p> </dd> <dt> <code class="parameter">local_only</code></dt> <dd> <p class="para"> Set to true to only return local environment variables (set by the operating system or putenv). </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.getenv-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the value of the environment variable <code class="parameter">varname</code>, or <strong><code>FALSE</code></strong> if the environment variable <code class="parameter">varname</code> does not exist. If <code class="parameter">varname</code> is omitted, all environment variables are returned as associative <span class="type"><a href="language.types.array.html" class="type array">array</a></span>. </p> </div> <div class="refsect1 changelog" id="refsect1-function.getenv-changelog"> <h3 class="title">Changelog</h3> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>7.1.0</td> <td> The <code class="parameter">varname</code> can now be omitted to retrieve an associative <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of all environment variables. </td> </tr> <tr> <td>5.5.38, 5.6.24, 7.0.9</td> <td> The <code class="parameter">local_only</code> parameter has been added. </td> </tr> </tbody> </table> </div> <div class="refsect1 notes" id="refsect1-function.getenv-notes"> <h3 class="title">Notes</h3> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> If PHP is running in a SAPI such as Fast CGI, this function will always return the value of an environment variable set by the SAPI, even if <span class="function"><a href="function.putenv.html" class="function">putenv()</a></span> has been used to set a local environment variable of the same name. Use the <code class="parameter">local_only</code> parameter to return the value of locally-set environment variables. </p> </div> </div> <div class="refsect1 examples" id="refsect1-function.getenv-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-539"> <p><strong>Example #1 <span class="function"><strong>getenv()</strong></span> 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: #FF8000">// Example use of getenv()<br /></span><span style="color: #0000BB">$ip </span><span style="color: #007700">= </span><span style="color: #0000BB">getenv</span><span style="color: #007700">(</span><span style="color: #DD0000">'REMOTE_ADDR'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Or simply use a Superglobal ($_SERVER or $_ENV)<br /></span><span style="color: #0000BB">$ip </span><span style="color: #007700">= </span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'REMOTE_ADDR'</span><span style="color: #007700">];<br /><br /></span><span style="color: #FF8000">// Safely get the value of an environment variable, ignoring whether <br />// or not it was set by a SAPI or has been changed with putenv<br /></span><span style="color: #0000BB">$ip </span><span style="color: #007700">= </span><span style="color: #0000BB">getenv</span><span style="color: #007700">(</span><span style="color: #DD0000">'REMOTE_ADDR'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">) ?: </span><span style="color: #0000BB">getenv</span><span style="color: #007700">(</span><span style="color: #DD0000">'REMOTE_ADDR'</span><span style="color: #007700">)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.getenv-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.putenv.html" class="function" rel="rdfs-seeAlso">putenv()</a> - Sets the value of an environment variable</span></li> <li class="member"><span class="function"><a href="function.apache-getenv.html" class="function" rel="rdfs-seeAlso">apache_getenv()</a> - Get an Apache subprocess_env variable</span></li> <li class="member"><a href="language.variables.superglobals.html" class="link">Superglobals</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.get-resources.html">get_resources</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.getlastmod.html">getlastmod</a></div> <div class="up"><a href="ref.info.html">PHP Options/Info Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>