Professional Documents
Culture Documents
Use of Getenv Function in PHP Fahmida Yesmin Marielle Sarong
Use of Getenv Function in PHP Fahmida Yesmin Marielle Sarong
Excerpt: In PHP, environment variables are used to set up applications and retrieve various
types of data from the server. One of the two ways to read environment variables in PHP is
using the getenv() function that is used with or without argument. Further explanation on this
topic is discussed here.
Permalink: use-of-getenv()-function-in-php
category: 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.
array getenv()
According to the above syntax, the getenv() function can be called without any argument.
<?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.
Output:
The following output will appear after running the script from the server. It shows the values of
the four environment variables.
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.