Переменные окружения linux для php

$_ENV

An associative array of variables passed to the current script via the environment method.

These variables are imported into PHP’s global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell’s documentation for a list of defined environment variables.

Other environment variables include the CGI variables, placed there regardless of whether PHP is running as a server module or CGI processor.

Examples

Example #1 $_ENV example

Assuming «bjori» executes this script

The above example will output something similar to:

Notes

Note:

This is a ‘superglobal’, or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

See Also

User Contributed Notes 2 notes

If your $_ENV array is mysteriously empty, but you still see the variables when calling getenv() or in your phpinfo(), check your http://us.php.net/manual/en/ini.core.php#ini.variables-order ini setting to ensure it includes «E» in the string.

Please note that writing to $_ENV does not actually set an environment variable, i.e. the variable will not propagate to any child processes you launch (except forked script processes, in which case it’s just a variable in the script’s memory). To set real environment variables, you must use putenv().

Basically, setting a variable in $_ENV does not have any meaning besides setting or overriding a script-wide global variable. Thus, one should never modify $_ENV except for testing purposes (and then be careful to use putenv() too, if appropriate).

PHP will not trigger any kind of error or notice when writing to $_ENV.

Источник

Environment Variables in PHP

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:
Result:

In case, you use the Windows OS; you have to open PowerShell and use the command:

Command:
Result:

Example of Using Environment Variables in PHP

He output will look something like:

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 in php.ini file.

Setting Environment Variables

Let us now discuss how to set an environment variable so that it can be made accessible from your PHP application.

Result:

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:

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.

» php -r ‘var_dump(getenv(«APP_ENV»));’

Result:
  • 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

Источник

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.

Читайте также:  What is linux integration services

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:

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.

//Call getenv() function without argument

echo «

The list of environment variables with values are :

» ;

//Print all environment variable names with values

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

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.

//Print the used language name

echo » Language: » . getenv ( «LANGUAGE» ) . «
» ;

//Print the used date and time formatting name

echo » Local Time: » . getenv ( «LC_TIME» ) . «
» ;

//Print the log directory name of the apache server

Читайте также:  Rust server linux install

echo » Apache Log Directory: » . getenv ( «APACHE_LOG_DIR» ) . «
» ;

//Print the values of PATH variable

echo » The values of PATH are: » . 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.

// Print the current value of REMOTE_ADDR

echo » The current Remote Address is : » . getenv ( «REMOTE_ADDR» ) . «
» ;

// Try to change the built-in REMOTE_ADDR variable

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

echo » The Remote Address after change is : » . getenv ( «REMOTE_ADDR» ) . «
» ;

// Define a custom environment variable

// Print the custom envirornment variable

echo «The value of MY_ENV_VAR is : » . 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.

Источник

Оцените статью
Adblock
detector