Environment Variables

Environment variables are a way to store and access configuration information or settings related to the runtime environment in which a PHP script is executed. These variables are part of the operating system’s environment and can be used to retrieve information about the server, the operating system, or custom configuration parameters that are set for a particular application. PHP provides a way to access these variables through the $_ENV superglobal array.

The environment variables are set in the operating system before the execution of the script, using commands such as export, setenv, or else (depending on the OS). They may also be provided at the start of containerized images.

<?php

// print all available environment variables at the start of the script
// it may be empty
print_r($_ENV);

// retrieve an environment variable
var_dump(getenv('MY_VARIABLE'));

// sets an environment variable
var_dump(setenv('MY_VARIABLE', 2));

?>

Documentation

See also Working with Environment Variables in PHP