symfony/dotenv

Registers environment variables from a .env file

PHP

3,790

509 commits

updated Oct 15, 2026

See the code

README

Dotenv Component

Symfony Dotenv parses .env files to make environment variables stored in them accessible via $_SERVER or $_ENV.

Getting Started

composer require symfony/dotenv

Usage

For an .env file with this format:

YOUR_VARIABLE_NAME=my-string
use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');

// you can also load several files
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');

// overwrites existing env variables
$dotenv->overload(__DIR__.'/.env');

// loads .env, .env.local, and .env.$APP_ENV.local or .env.$APP_ENV
$dotenv->loadEnv(__DIR__.'/.env');

// Usage with $_ENV
$envVariable = $_ENV['YOUR_VARIABLE_NAME'];

// Usage with $_SERVER
$envVariable = $_SERVER['YOUR_VARIABLE_NAME'];

This package is looking for a backer.

Help Symfony by sponsoring its development!

Resources

component
dotenv
env
environment
php
symfony
symfony-component

symfony/dotenv

Registers environment variables from a .env file

PHP

3,790

509 commits

updated Oct 15, 2026

See the code

README

Dotenv Component

Symfony Dotenv parses .env files to make environment variables stored in them accessible via $_SERVER or $_ENV.

Getting Started

composer require symfony/dotenv

Usage

For an .env file with this format:

YOUR_VARIABLE_NAME=my-string
use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');

// you can also load several files
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');

// overwrites existing env variables
$dotenv->overload(__DIR__.'/.env');

// loads .env, .env.local, and .env.$APP_ENV.local or .env.$APP_ENV
$dotenv->loadEnv(__DIR__.'/.env');

// Usage with $_ENV
$envVariable = $_ENV['YOUR_VARIABLE_NAME'];

// Usage with $_SERVER
$envVariable = $_SERVER['YOUR_VARIABLE_NAME'];

This package is looking for a backer.

Help Symfony by sponsoring its development!

Resources

component
dotenv
env
environment
php
symfony
symfony-component

Project health

Overall

4.0/10

Maintained

7/10

Code review

0/10
OpenSSF Scorecard

Significant stargazers

Carl Schwan

114 followers · starred Jun 2018

Derick Rethans

1,840 followers · starred Feb 2021

Hugo Alliaume

278 followers · starred Dec 2024

Malachi Soord

95 followers · starred Apr 2018