Using Environment variables in Wordpress wp-config

I prefer to use this approach below:

<?php

//GET HOSTNAME INFO
$hostname = $_SERVER['SERVER_NAME']; 

//VERIFY WHICH ENVIRONMENT THE APP IS RUNNING
switch ($hostname) {
    case 'development.dev':
        define('WP_ENV', 'development');
        define('WP_DEBUG', true);
        break;
    case 'staging.mywebsite.com':
        define('WP_ENV', 'staging');
        define('WP_DEBUG', true);
        break;
    case 'www.mywebsite.com':
        define('WP_ENV', 'production');
        define('WP_DEBUG', false);
        break;
    default:
        define('WP_ENV', 'production');
        define('WP_DEBUG', false);
}

?>

You could make it half as long by passing the function result as a constant value without intermediate variable:

define('AUTH_KEY', getenv('AUTH_KEY'));

Or do that in a loop:

$vars = array('AUTH_KEY', 'SECURE_AUTH_KEY', ...);
foreach ($vars as $var) {
    define($var, getenv($var));
}

From WordPress 5.5.0

WordPress has added a new function for the environment variables with 3 different possible values.

You can use wp_get_environment_type() function to get the current environment.

Usage example:

If(wp_get_environment_type() === 'development') {
 // do something
} else {
 // do something
}

By default, if WP_ENVIRONMENT_TYPE is empty or invalid ( anything except development, staging & production), production is returned.

You can define development or staging environment through the wp-config.php file.

define( 'WP_ENVIRONMENT_TYPE', 'development' );