Voir aussi

User Contributed Notes 11 notes

This function is useful (compared to $_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: <?php getenv('path') ?>

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.

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

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.

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