This tutorial has a new version, check it out!

dotenv: Environmental Variables

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

The $debug and $environment variables are hardcoded in index.php. So if we deploy this, we'll either have a web debug toolbar on production, or we'll need to manually modify this file to set $debug to false. Both situations stink.

PHP dotenv Fanciness

To help, we'll install a new library: composer require vlucas/phpdotenv:

composer require vlucas/phpdotenv

While we're waiting, Google for that library and find its documentation.

This is a popular library these days, and I really like it too. It allows you to have a .env file at the root of your project. It'll look something like this:


This library reads these values and turns S3_BUCKET and SECRET_KEY into environment variables. Then, in our app, whenever we need some configuration - like whether we're in debug mode, the database password or the S3 bucket - we'll read from the environment variables. When you eventually deploy, you can set those variables in two different ways. First, you can have a .env file. Or second, you can set the variables via something like your web server configuration. Some platforms - like Heroku also have a way to set environment variables. The important point is that your app isn't bound to a configuration file: it's just reading environment variables, which is a pretty standard way of setting config.

Our .env File

The first things we want to set are the $env and $debug flags. Create a .env file and say SYMFONY_ENV=dev and SYMFONY_DEBUG=1:

4 lines .env
# basic setup

Remove the old variables in index.php. Replace it with $dotenv = new DotEnv\DotEnv(). The argument is the directory where the .env file lives - it's actually up one directory from here. Then, call $dotenv->load():

26 lines web/index.php
... lines 1 - 9
// load the environmental variables
$dotenv = new Dotenv\Dotenv(__DIR__.'/../');
... lines 13 - 26

At this point, those two flags have been set as environment variables. That means we can say $env = $_SERVER['SYMFONY_ENV']; and $debug = $_SERVER['SYMFONY_DEBUG'];:

26 lines web/index.php
... lines 1 - 9
// load the environmental variables
$dotenv = new Dotenv\Dotenv(__DIR__.'/../');
... lines 15 - 26

Go back and refresh the new setup - we should still see the toolbar. Yep, there it is. But now go back to .env and set SYMFONY_DEBUG to 0. Because of config.yml, this should turn the toolbar off. Change the environment to prod too - that's not being used anywhere yet, but it may avoid a temporary cache error:

4 lines .env
# basic setup

Try it out: no web debug toolbar.

Add .env to the .gitignore file - this shouldn't be committed:

5 lines .gitignore
... lines 1 - 3

But copy .env to .env.example - we will commit this so that new developers have something they can use as a guide.

Leave a comment!

  • 2019-02-18 weaverryan

    Yes, great resource! Thanks for sharing!

  • 2019-02-18 El Tebe

    Thank you so much :)

    I found some useful info too about env variables on Fabien's blog site. See the "Environment Variables" part here:

  • 2019-02-14 weaverryan

    Yo El Tebe!

    Ah, great question! In practice, very little :). This tutorial pre-dates Symfony 4 and Symfony Flex by quite a long time. But some of its ideas were eventually used in Symfony 4, and this is one of them. In this tutorial, SYMFONY_ENV becomes the Symfony environment and in Symfony 4, APP_ENV becomes the Symfony environment. There are some subtle differences, but basically they do the same thing. The same is true for the DEBUG env vars, except that you don't *need* to define APP_DEBUG in Symfony 4 - if you don't define it, it's "guessed" from the APP_ENV value.


  • 2019-02-13 El Tebe

    What is the difference? Which environment var used by Symfony 4.x?


  • 2017-05-05 weaverryan

    Yo Caim Astraea!

    I don't know the answer specifically, but I do know that the DriverManager you linked to uses parse_url to break the URL into pieces. So that should, in theory, work fine. However, you might need to urlencode some of the special characters in your password. Check out the latest commit to that class, which add support for this:, and the PR behind it:


  • 2017-05-04 Caim Astraea

    hmm weird it works with root user without password
    seems the parseDatabaseUrl function doesn't like that url string.

  • 2017-05-04 Caim Astraea

    Anyone knows how to define the databse url ? Been trying out
    but it doesn't seem to like the url my mysql also uses a password unlike the example.
    Tried like this DATABASE_URL=mysql://newuser:Dorobanti71#$@
    but am getting
    DBALException in DriverManager.php line 259:
    Malformed parameter "url".
    in DriverManager.php line 259
    at DriverManager::parseDatabaseUrl(array('url' => 'mysql://newuser:Dorobanti71#$@', 'host' => 'localhost', 'port' => null, 'user' => 'root', 'password' => null, 'driver' => 'pdo_mysql', 'driverOptions' => array(), 'defaultTableOptions' => array())) in DriverManager.php line 144