php

Use of getenv() function in PHP

Environment variables are used in PHP to set up the application and retrieve the different types of data dynamically from the server. The database credentials, API keys, etc., are visible to the code through the environment variable instead of using any configuration file. When any PHP script runs, then it will inherit all required environment variables from the server. There are two ways to read environment variables in PHP. One is getenv() function and another is $_ENV array. The uses of the getenv() function are shown in this tutorial.

Syntax:

getenv() function can be used with or without an argument. When this function uses the arguments, it returns the string value, and when the function uses no argument, it returns an array. Both syntaxes of this function are shown below:

string getenv ( string $varname [, bool $local_only = false ] )

According to the above syntax, the getenv () function can take two arguments. The first argument is mandatory and is used to take the environment variable’s name that is required to read. The second argument is optional with a default value of FALSE. When TRUE is used in the second argument, this function will return the local environment variables only.

According to the above syntax, the getenv() function can be called without any argument.

Example 1: Use of getenv() variable without argument

The following example will show the list of environment variables of the installed version of PHP. Create a PHP file with the following script to get the list of environment variables using the getenv() function.

getenv() function is called without any argument in the script, and the returned values are stored in an array named $env_array. The values of this array are printed using the foreach loop.

<?php

//Call getenv() function without argument

$env_array =getenv();

echo "<h3>The list of environment variables with values are :</h3>";

//Print all environment variable names with values

foreach ($env_array as $key=>$value)

{

    echo "$key => $value <br />";

}

?>

Output:

The following output will appear after running the script from the server. It shows the list of all environment variables of the PHP. This output can vary based on the PHP version and the operating system, wherein the PHP is running.

Example 2: Read the specific environment variables

The following example shows the way to read the particular environment values. Create a PHP file with the following script.

The four environment variables are printed using the getenv() function. “LANGUAGE” is used in the getenv() function to read which language is currently set for the PHP script. “LC_TIME” is used in the getenv() function to read the used date and time formatting name in PHP. “APACHE_LOG_DIR” is used in the getenv() function to read the log directory of Apache. “PATH” is used in the getenv() function to read the values stored in the path.

<?php

//Print the used language name

echo "<b> Language: </b>" . getenv("LANGUAGE"). "<br />";

//Print the used date and time formatting name

echo "<b> Local Time: </b>" . getenv("LC_TIME"). "<br />";

//Print the log directory name of the apache server

echo "<b> Apache Log Directory: </b>" . getenv("APACHE_LOG_DIR"). "<br />";

//Print the values of PATH variable

echo "<b> The values of PATH are: </b>" . getenv("PATH");

?>

Output:

The following output will appear after running the script from the server. It shows the values of the four environment variables.

Example 3: Define and read environment variable

getenv() function returns the list of built-in environment variables of the PHP. But if the coder needs to create any new environment variable for the programming purpose, they can do so. putenv() function can be used to create a new environment variable with a value. To create a new environment variable, the variable name, equal sign(=), and the value of the variable are enclosed with the quotation to be used as the argument value of the putenv() function. But the value of any built-in environment variable can’t be changed using the putenv() function.

The following example shows the way to create a new environment variable using the putenv() function and reads the newly created environment variable using the getenv() function. Create a PHP file with the following script.

“REMOTE_ADDR” is a built-in environment variable name with a value that is printed at the beginning of the script. Next, a new value is set for this variable and printed again. A new environment variable named “MY_ENV_VAR” is created with a value and printed later.

<?php

// Print the current value of REMOTE_ADDR

echo "<b> The current Remote Address is : </b>" . getenv("REMOTE_ADDR"). "<br />";

// Try to change the built-in REMOTE_ADDR variable

putenv("REMOTE_ADDR=localserver");

// Print the value of REMOTE_ADDR after using putenv()

echo "<b> The Remote Address after change is : </b>" . getenv("REMOTE_ADDR"). "<br />";


// Define a custom environment variable

putenv("MY_ENV_VAR=TestSrver");

// Print the custom envirornment variable

echo "<b>The value of MY_ENV_VAR is : </b>" . getenv("MY_ENV_VAR");

?>

Output:

The following output will appear after running the script from the server. It shows that the default value of “REMOTE_ADDR” is 127.0.0.1. When the value of this environment variable is changed and re-printed, it will show its previous value. That means the value of the built-in variables can’t be changed. The newly created environment variable is printed properly here.

Conclusion

The ways of reading built-in environment variables and creating a new environment variable are shown in this tutorial using different examples. There is a superglobal variable named $_ENV that can also be used to read the environment variable of PHP. The concept of environment variables in PHP will be cleared after reading this tutorial, and PHP coders will be able to use these variables in their scripts.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.