PHP: Hypertext Preprocessor (original) (raw)

getenv

(PHP 4, PHP 5, PHP 7, PHP 8)

getenv — Gets the value of a single or all environment variables

Description

You can see a list of all the environmental variables by usingphpinfo(). Many of these variables are listed within» RFC 3875, specifically section 4.1, "Request Meta-Variables".

Parameters

name

The variable name as a string or [null](reserved.constants.php#constant.null).

local_only

When set to [true](reserved.constants.php#constant.true), only local environment variables are returned, set by the operating system or putenv. It only has an effect when name is a string.

Return Values

Returns the value of the environment variablename, or [false](reserved.constants.php#constant.false) if the environment variable name does not exist. If name is [null](reserved.constants.php#constant.null), all environment variables are returned as an associative array.

Changelog

Version Description
8.0.0 The name is now nullable.
7.1.0 The name can now be omitted to retrieve an associative array of all environment variables.
7.0.9 The local_only parameter has been added.

Examples

Example #1 getenv() Example

<?php // Example use of getenv() <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>p</mi><mo>=</mo><mi>g</mi><mi>e</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>v</mi><msup><mo stretchy="false">(</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi>R</mi><mi>E</mi><mi>M</mi><mi>O</mi><mi>T</mi><msub><mi>E</mi><mi>A</mi></msub><mi>D</mi><mi>D</mi><msup><mi>R</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo stretchy="false">)</mo><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi>O</mi><mi>r</mi><mi>s</mi><mi>i</mi><mi>m</mi><mi>p</mi><mi>l</mi><mi>y</mi><mi>u</mi><mi>s</mi><mi>e</mi><mi>a</mi><mi>S</mi><mi>u</mi><mi>p</mi><mi>e</mi><mi>r</mi><mi>g</mi><mi>l</mi><mi>o</mi><mi>b</mi><mi>a</mi><mi>l</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">ip = getenv(&#x27;REMOTE_ADDR&#x27;);// Or simply use a Superglobal (</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mopen"><span class="mopen">(</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.13889em;">REMOT</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.05764em;">E</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3283em;"><span style="top:-2.55em;margin-left:-0.0576em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">A</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal" style="margin-right:0.02778em;">DD</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.00773em;">R</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mclose">)</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//</span><span class="mord mathnormal" style="margin-right:0.02778em;">O</span><span class="mord mathnormal">rs</span><span class="mord mathnormal">im</span><span class="mord mathnormal" style="margin-right:0.01968em;">pl</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal">u</span><span class="mord mathnormal">se</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.05764em;">S</span><span class="mord mathnormal">u</span><span class="mord mathnormal">p</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">o</span><span class="mord mathnormal">ba</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mopen">(</span></span></span></span>_SERVER or $_ENV) <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>p</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">ip = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">p</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>_SERVER['REMOTE_ADDR'];// Safely get the value of an environment variable, ignoring whether // or not it was set by a SAPI or has been changed with putenv $ip = getenv('REMOTE_ADDR', true) ?: getenv('REMOTE_ADDR') ?>

Notes

Warning

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 putenv() has been used to set a local environment variable of the same name. Use the local_only parameter to return the value of locally-set environment variables.

See Also

Found A Problem?

Anonymous

7 years ago

`Contrary to what eng.mrkto.com said, getenv() isn't always case-insensitive. On Linux it is not:

SERVER,_SERVER, SERVER,_ENV) because it searches $varname key in those array case-insensitive manner. For example on Windows $_SERVER['Path'] is like you see Capitalized, not 'PATH' as you expected. So just:

`

jcastromail at yahoo dot es

4 years ago

`I did a benchmark about env.

constants :
0.00067687034606934 ms
getenv :
0.056761026382446 ms

(less is better)

https://github.com/eftec/php-benchmarks#define--const--env

And, in Windows at leat, reading the env value is considerably slow (in comparison with a constant), so PHP doesn't cache the information and asks to the OS the env value per call.

So, if you are calling once per request, then there is not a problem. However, if you are calling it many times per request, then it could affects the performance.

`

php at keith tyler dot com

13 years ago

`All of the notes and examples so far have been strictly CGI.
It should not be understated the usefulness of getenv()/putenv() in CLI as well.

You can pass a number of variables to a CLI script via environment variables, either in Unix/Linux bash/sh with the "VAR='foo'; export $VAR" paradigm, or in Windows with the "set VAR='foo'" paradigm. (Csh users, you're on your own!) getenv("VAR") will retrieve that value from the environment.

We have a system by which we include a file full of putenv() statements storing configuration values that can apply to many different CLI PHP programs. But if we want to override these values, we can use the shell's (or calling application, such as ant) environment variable setting method to do so.

This saves us from having to manage an unmanageable amount of one-off configuration changes per execution via command line arguments; instead we just set the appropriate env var first.

`

kyong

21 years ago

As you know, getenv('DOCUMENT_ROOT') is useful. However, under CLI environment(I tend to do quick check if it works or not), it doesn't work without modified php.ini file. So I add "export DOCUMENT_ROOT=~" in my .bash_profile.

pritisn at gmail dot com

9 years ago

for quick check of getenv() adding a new env variable - if you add a new env variable, make sure not only apache but xampp is also restarted. Otherwise getenv() will return false for the newly added env variable.

Anonymous

5 years ago

It is worth noting that since getenv('MY_VARIABLE') will return false when the variable given is not set, there is no direct way to distinguish between a variable that is unset and one that is explicitly set to the value bool(false) when using getenv(). This makes it somewhat tricky to have boolean environment variables default to true if unset, which you can work around either by using "falsy" values such as 0 with the strict comparison operators or by using the superglobal arrays and isset().

sam at sambarrow dot com

17 years ago

SERVER_NAME is the name defined in the apache configuration. HTTP_HOST is the host header sent by the client when using the more recent versions of the http protocol.

hello at jabran dot me

10 years ago

Beware that when using this function with PHP built-in server – i.e. php -S localhost:8000 – it will return boolean FALSE.

chuck dot reeves at gmail dot com

14 years ago

When writing CLI applications, not that any environment variables that are set in your web server config will not be passed through. PHP will pass through system environment variables that are prefixed based off the safe_mode_allowed_env_vars directive in your php.ini