https://github.com/rayanlevert/dotenv
Dependency-free environment file handler (.env) loading variables and its values in $_ENV, $_SERVER and getenv() in the PHP userland
https://github.com/rayanlevert/dotenv
dependency-free dotenv dotenv-loader dotenv-parser php
Last synced: 3 months ago
JSON representation
Dependency-free environment file handler (.env) loading variables and its values in $_ENV, $_SERVER and getenv() in the PHP userland
- Host: GitHub
- URL: https://github.com/rayanlevert/dotenv
- Owner: rayanlevert
- License: bsd-3-clause
- Created: 2023-12-01T07:45:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-08-16T04:53:06.000Z (8 months ago)
- Last Synced: 2025-10-13T04:12:44.830Z (6 months ago)
- Topics: dependency-free, dotenv, dotenv-loader, dotenv-parser, php
- Language: PHP
- Homepage:
- Size: 71.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Simple, dependency-free and fast class handling an environment file to `$_ENV`, `$_SERVER` and `getenv()`
[](https://packagist.org/packages/rayanlevert/dotenv)
[](https://packagist.org/packages/rayanlevert/dotenv)
[](https://codecov.io/gh/rayanlevert/dotenv)
> Version >= 3.0 supports only php8.4 with all brand new features, for >= php8.1, version 2.0 is still supported.
### Initializes the instance setting the file path
```php
$oDotenv = new \RayanLevert\Dotenv\Dotenv('/file/to/.dotenv');
```
An exception `RayanLevert\Dotenv\Exception` will be thrown if the file is not readable
### Reads the file content and loads in `$_ENV`, `$_SERVER` et `getenv()`, values of each variable
```php
$oDotenv->load();
```
#### For each new line found, tries to set to the name of the variable its value after the `=` sign
```
TEST_VALUE1=value1 => $_ENV['TEST_VALUE1'] = value1
```
## If the value is a primitive value, the value will be casted to the `PHP` userland
```php
NAME=1 => $_ENV['NAME'] = 1
NAME=23.34 => $_ENV['NAME'] = 23.34 (float values will be casted only with a dot .)
NAME=true => $_ENV['NAME'] = true
NAME=false => $_ENV['NAME'] = false
NAME=string value => $_ENV['NAME'] = 'string value'
```
### Multiline variables are also available ! (separated by `\n`), double quotes (`"`) will be used
```php
NAME="This is a variable
with
multiple
lines"
```
### Nested variables, declared beforehand via the same file or `getenv()` (set via the OS or docker for example)
```php
NESTED=VALUE
NAME=${NESTED}
NAME2=${NESTED}/path
$_ENV['NESTED'] = 'VALUE'
$_ENV['NAME'] = 'VALUE'
$_ENV['NAME2'] = 'VALUE/path'
```
Throw an `RayanLevert\Dotenv\Exception` if at least one variable is not present in the `$_ENV` superglobal
```php
$oDotenv->required('FIRST_REQUIRED', 'SECOND_REQUIRED');
```
Worth if we want required variables for application purposes, an exception will be thrown to prevent some logic error