chillerlan/php-dotenv

A simple .env loader - PHP 8.1+

3.0.1 2024-03-07 20:36 UTC

This package is auto-updated.

Last update: 2024-10-07 21:47:48 UTC


README

Loads contents from a .env file into the environment. PHP 8.1+

PHP Version Support version license Coverage Packagist downloads CI

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