<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.info.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.getenv.php',
    1 => 'getenv',
    2 => 'Gets the value of a single or all environment variables',
  ),
  'up' => 
  array (
    0 => 'ref.info.php',
    1 => 'PHP Options/Info Functions',
  ),
  'prev' => 
  array (
    0 => 'function.get-resources.php',
    1 => 'get_resources',
  ),
  'next' => 
  array (
    0 => 'function.getlastmod.php',
    1 => 'getlastmod',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/info/functions/getenv.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.getenv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">getenv</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">getenv</span> &mdash; <span class="dc-title">Gets the value of a single or all environment variables</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.getenv-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>getenv</strong></span>(<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$name</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$local_only</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Gets the value of a single or all environment variables.
  </p>
  <p class="para">
   You can see a list of all the environmental variables by using
   <span class="function"><a href="function.phpinfo.php" class="function">phpinfo()</a></span>. Many of these variables are listed within
   <a href="https://datatracker.ietf.org/doc/html/rfc3875" class="link external">&raquo;&nbsp;RFC 3875</a>, specifically
   section 4.1, &quot;Request Meta-Variables&quot;.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.getenv-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">name</code></dt>
     <dd>
      <p class="para">
       The variable name as a <span class="type"><a href="language.types.string.php" class="type string">string</a></span> or <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">local_only</code></dt>
     <dd>
      <p class="para">
       When set to <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>, only local environment variables are returned, set by the operating system or putenv. It only has
       an effect when <code class="parameter">name</code> is a <span class="type"><a href="language.types.string.php" class="type string">string</a></span>.
      </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">name</code>, or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> if the environment
   variable <code class="parameter">name</code> does not exist.
   If <code class="parameter">name</code> is <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, all environment variables are
   returned as an associative <span class="type"><a href="language.types.array.php" 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>8.0.0</td>
      <td>
       The <code class="parameter">name</code> is now nullable.
      </td>
     </tr>

     <tr>
      <td>7.1.0</td>
      <td>
       The <code class="parameter">name</code> can now be omitted to retrieve an
       associative <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of all environment variables.
      </td>
     </tr>

     <tr>
      <td>7.0.9</td>
      <td>
       The <code class="parameter">local_only</code> parameter has been added.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.getenv-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <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">&lt;?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">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </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.php" 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 seealso" id="refsect1-function.getenv-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.putenv.php" class="function" rel="rdfs-seeAlso">putenv()</a> - Sets the value of an environment variable</span></li>
    <li><span class="function"><a href="function.apache-getenv.php" class="function" rel="rdfs-seeAlso">apache_getenv()</a> - Get an Apache subprocess_env variable</span></li>
    <li><a href="language.variables.superglobals.php" class="link">Superglobals</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>