Environment Variables in PHP
Environment variables are a universal mechanism for conveying configuration information to Unix programs. Let’s look at how to set, get, and list environment variables.
Running the program shows that we pick up the value for FOO
that we set in the program, but that BAR
is empty.
The list of keys in the environment will depend on your particular machine.
If we set BAR
in the environment first, the running program picks that value up.
In PHP, you can also access environment variables directly using the $_ENV
superglobal array, although this might require changes to your php.ini
configuration. The getenv()
function is generally more reliable across different PHP configurations.
Remember that changes made to the environment using putenv()
only affect the current process and its child processes. They do not modify the system environment permanently.