phpsymfonysymfony4

How to get the value of a key from the .env file?


In the .env file there are settings of key values; for example : APP_ENV=prod

How to get the value of the key APP_ENV for example ?


Solution

  • See https://github.com/symfony/dotenv / https://symfony.com/doc/4.1/components/dotenv.html, which parses into $_ENV, and PHP has a getenv function which you use to get the value out of.

    use Symfony\Component\Dotenv\Dotenv;
    
    $dotenv = new Dotenv();
    $dotenv->load(__DIR__.'/.env');
    
    $appEnv = getenv('APP_ENV');
    // you can also use ``$_ENV`` or ``$_SERVER``