W3docs

Get environment value in controller

To get an environment value in a PHP controller, you can use the getenv function.

To get an environment value in a PHP controller, you can use the getenv function. For example, to get the value of the APP_ENV environment variable, you would do the following:

Example of getting an environment value in a PHP controller

$appEnv = getenv('APP_ENV') ?: 'production';

This will return the value of the APP_ENV environment variable as a string, or a fallback value if the variable is not set.

You can also use the $_ENV superglobal to get environment variables. For example:

Example of using $_ENV superglobal to get environment variables in a PHP controller

$appEnv = $_ENV['APP_ENV'] ?? 'production';

The $_ENV superglobal is an array that contains all environment variables as elements. Note that $_ENV may be empty if the variables_order directive in php.ini does not include E.

Environment variables are specific to the environment in which a PHP script is executed, and are often used to configure PHP scripts for different environments (e.g. development, staging, production). They can be set in the system environment, or in a file such as .env in the root of your project. When using a framework like Symfony, consider using the framework's dedicated environment helper (e.g., env()) instead of raw PHP functions.