chillerlan / php-dotenv
A simple .env loader - PHP 8.1+
Fund package maintenance!
Ko Fi
www.paypal.com/donate?hosted_button_id=WLYUNAT9ZTJZ4
Installs: 10 975
Dependents: 11
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- phan/phan: ^5.4
- phpunit/phpunit: ^10.5
README
Loads contents from a .env
file into the environment. PHP 8.1+
Documentation
Installation
requires composer
composer.json (note: replace dev-main
with a version constraint, e.g. ^3.0
)
{ "require": { "php": "^8.1", "chillerlan/php-dotenv": "dev-main" } }
Installation via terminal: composer require chillerlan/php-dotenv
Profit!
Usage
# example .env
FOO=bar
BAR=foo
WHAT=${BAR}-${FOO}
$env = new DotEnv(__DIR__.'/../config', '.env'); $env->load(['foo']); // foo is required // get a variable $foo = $_ENV['FOO']; // -> bar $foo = $env->get('FOO'); // -> bar $foo = $env->FOO; // -> bar // dynamically set a variable $env->set('foo', 'whatever'); $env->FOO = 'whatever'; $foo = $env->get('FOO'); // -> whatever // ... // variable substitution $foo = $env->get('WHAT'); // -> foo-bar
// avoid the global environment $env = (new DotEnv(__DIR__.'/../config', '.env', false))->load(); $foo = $_ENV['FOO']; // -> undefined $foo = $env->get('FOO'); // -> bar $foo = $env->FOO; // -> bar