The most effective practice for application setup is by using PHP environment variables, whether its database credentials, secret data parameters, API keys, or anything between deploys are now visible to code through the environment. The PHP environment variable allows developers to gather specific types of data from existing servers dynamically. In this tutorial, you will learn how to use PHP environment variables and what their features are.
PHP Environment Variables
Various PHP frameworks such as Laravel, Symfony, and others use the PHP environment variable itself to store different security-related credentials and other configurations. An ENV var or environment variable is nothing but a key-value pair used in the global scope. These variables are explicitly stored for each environment. In other words, an environment variable can be defined as a dynamic-named variable that is provided in a program for affecting the way successively running processes will work in a system.
These variables are brought into the global namespace of PHP from the environment under which the PHP runs its parser. Many of them are provided by the shell under which PHP runs with different systems that are likely to run different kinds of shells. Other environment variables include the CGI variables, irrespective of whether they are running as a server module or as a CGI processor in PHP.
More About PHP Environment Variables
As a process starts in a program, it uses its defined variables or inherits from the parent process. These variables are used for discovering facts about the environment on which it is running. These variables include details about the preferred location where the temporary files are being saved or the path where the home directory resides within the system.
If you use a Unix operating system such as Linux, you can see this by typing the value of the $HOME environment variable in the terminal:
Command:
» echo $HOME
Result:
/Users/matt
In case, you use the Windows OS; you have to open PowerShell and use the command:
Command:
Write-Output $env:HOMEPATH
Result:
C:\Users\Gautam>
Example of Using Environment Variables in PHP
Example:
<?php
echo 'The username is: ' .$_ENV["USER"] . '!';
?>
He output will look something like:
The username is Alex!
Why $_ENV Is Empty
I want to use $_ENV so that I can get the username of the logged-in user, but it is empty?
Everyone should use the getenv() function instead, but if you require $_ENV, you need to do so. To use $_ENV, you must activate it in your php.ini file. Find "variable_orders" and set it to:
variables_order = "GPCSE"
Setting Environment Variables
Let us now discuss how to set an environment variable so that it can be made accessible from your PHP application.
» php -r 'var_dump(getenv("HOME"));'
Result:
string(13) "/Users/Alex"
But in case you want to include your variables in a PHP program, the simplest way to do this is to state the environment variable well before your run command, something like this:
» APP_ENV=local php -r 'var_dump(getenv("APP_ENV"));'
Result:
string(5) "local"
Another well-known &convenient approach used in Unix systems is to make use of the "export" command. When the'export' is used with an environment variable, it will be available in all successive commands until the shell exits.
» export APP_ENV=local
» php -r 'var_dump(getenv("APP_ENV"));'
Result:
string(5) "local"
Related Functions of PHP Environment Variable
Since environment variables are global variables, two commonly used functions are used for setting and getting the environment variables. These are:
- getenv() is a PHP function used for returning the specific environment variable's value
- putenv() is a PHP function that is used for setting the value of a particular environment variable
Code Snippet for getenv():
Example:
<?PHP
getenv("REMOTE_ADDR"); //fetch the current user's IP address
putenv("tmp=usr"); //set the user's value for environment variable
getenv("tmp");
?>
Code Snippet for putenv():
Example:
<?PHP
$_SERVER['REMOTE_ADDR']; //find the current user's IP address
putenv("tmp=usr"); //set an value of environment variable
echo $_SERVER['tmp'];
?>