chillerlan/php-dotenv

A simple .env loader - PHP 7.2+

1.0.0 2018-09-07 03:59 UTC

README

Loads contents from a .env file into the environment (similar to vlucas/phpdotenv). PHP 7.2+

version license Travis Coverage Scrunitizer Packagist downloads PayPal donate

Documentation

Installation

requires composer

composer.json (note: replace dev-master with a version boundary)

{
	"require": {
		"php": "^7.2",
		"chillerlan/php-dotenv": "^1.0"
	}
}

Manual installation

Download the desired version of the package from master or release and extract the contents to your project folder. After that:

  • run composer install to install the required dependencies and generate /vendor/autoload.php.
  • if you use a custom autoloader, point the namespace chillerlan\DotEnv to the folder src of the package

Profit!

Usage

# example .env
FOO=bar
BAR=foo
WHAT=${BAR}-${FOO}
$env = new DotEnv(__DIR__.'/../config', '.env');
$env->load(['foo']); // foo is required

$foo = $env->get('FOO'); // -> bar
$foo = $_ENV['FOO']; // -> bar

$foo = $env->set('foo', 'whatever');
$foo = $env->get('FOO'); // -> whatever
// avoid the global environment
$env = (new DotEnv(__DIR__.'/../config', '.env', false))->load();

$foo = $env->get('FOO'); // -> bar
$foo = $_ENV['FOO']; // -> undefined