Dotenv component parses .env
files and add them to both $_ENV
and
$_SERVER
.
$ composer require climbx/dotenv
# .env
foo=bar
bar=baz
// PHP
use Climbx\Dotenv\Dotenv;
$dotenv = new Dotenv();
$envPath = __DIR__ . '/.env';
/*
* Loads env vars into $_ENV and $_SERVER
* without overwriting existing variables
*/
$dotenv->load($envPath);
/*
* Loads env vars into $_ENV and $_SERVER
* if a variable already exists, it is overridden.
*/
$dotenv->overload($envPath);
# Comment line
foo=bar # End line comment
bar='this is a single quotted value' # allows whitespaces
baz="this is a double quotted value"
num=1234
Simple var reference:
foo=bar
baz=$foo # outputs ['baz' => 'bar']
Multiple var references:
foo=1
bar=2
spaced="$foo and $bar" # outputs ['spaced' => '1 and 2']
collapsed=$one$two # outputs ['collapsed' => '12']
Partial var references:
foo="one"
bar="two"
baz=${foo}/${bar}/three # outputs ['baz' => 'one/two/three']
Escaped var declaration char
foo=bar
echaped1=\$foo # outputs ['echaped1' => '$foo']
echaped2=\${foo} # outputs ['echaped2' => '${foo}']
Missing var reference.
Missing references don't throw an exception. If a reference is not found,
the value is set to an empty string.
missing1=$foo # outputs ['missing1' => '']
missing2=${foo} # outputs ['missing2' => '']