Download the PHP package laxity7/dotenv without Composer
On this page you can find all versions of the php package laxity7/dotenv. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download laxity7/dotenv
More information about laxity7/dotenv
Files in laxity7/dotenv
Package dotenv
Short Description Registers environment variables from a .env file
License MIT
Homepage https://github.com/laxity7/dotenv
Informations about the package dotenv
PHP DotEnv
A fast, lightweight .env file parser for PHP. Loads environment variables and makes them accessible
via $_ENV, getenv(), and the built-in Env::get() helper.
Table of Contents
- Requirements
- Installation
- Quick Start
- Features
- Type Casting
- Variable Interpolation
- Export Prefix
- Inline Comments
- Error Tolerance
- API
- DotEnv
- Env Helper
- Comparison with symfony/dotenv
- Development
Requirements
- PHP >=7.4 | 8+
Installation
Quick Start
Create a .env file in your project root (make sure it is added to .gitignore):
Load it in your application:
All loaded variables are also available in $_ENV:
If the file does not exist, no error is thrown — only already existing environment variables will be available.
You can load additional files. By default, existing variables are not overwritten:
Helper Function
For convenience, you can create a global env() helper:
Or use the built-in Env static helper:
Or combine both approaches for a short env() function:
Features
Type Casting
Values are automatically cast to native PHP types:
| .env value | PHP type | PHP value |
|---|---|---|
FOO=hello |
string |
'hello' |
FOO=123 |
int |
123 |
FOO=3.14 |
float |
3.14 |
FOO=.5 |
float |
0.5 |
FOO=true / FOO=false |
bool |
true/false |
FOO=null |
null |
null |
FOO= |
null |
null |
FOO="true" |
string |
'true' |
FOO="123" |
string |
'123' |
Note: Quoted values (
"..."or'...') are always treated as strings, even if they look like numbers or booleans.
Variable Interpolation
Reference previously defined variables using ${VAR} syntax:
Interpolation is not performed inside single-quoted values:
FOO='${BAR}'will remain the literal string${BAR}.
If a referenced variable is not defined, it is replaced with an empty string.
Export Prefix
Lines prefixed with export are supported (common in shell-compatible .env files):
The export keyword is simply stripped — variables are loaded as usual.
Inline Comments
Comments are supported both on their own line and inline:
Error Tolerance
Unlike many parsers, this package gracefully handles common formatting issues:
All of the above will be parsed without errors.
API
DotEnv
load(string $path, bool $overrideExistingVars = false, bool $usePutEnv = false): void
Load variables from a .env file.
$path— path to the.envfile. If the file does not exist, the call is silently ignored.$overrideExistingVars— iftrue, variables from the file will overwrite already defined ones.$usePutEnv— iftrue, also callputenv()for each variable (see below).
get(string $name, $default = null): mixed
Get the value of an environment variable. Returns $default if the variable is not defined.
has(string $name): bool
Check whether a variable exists (including variables with a null value).
getAll(): array
Return all loaded environment variables as an associative array.
clear(): void
Clear the internal variable store. Does not modify $_ENV or system environment — only resets the internal state.
$usePutEnv (property)
When enabled, variables are also registered via putenv(), making them accessible through getenv().
⚠️
putenv()is not thread-safe. Use only when you specifically needgetenv()support.
The $usePutEnv parameter in load() serves the same purpose but applies only to that single call,
while the property applies to all subsequent load() calls.
Env Helper
A static helper class for convenient access from anywhere in your application.
Calling
get()orhas()beforeload()will throw a\RuntimeException.
Comparison with symfony/dotenv
| Feature | laxity7/dotenv | symfony/dotenv |
|---|---|---|
| Native boolean casting | ✅ | ❌ |
| Native int/float casting | ✅ | ❌ |
| Error tolerance | ✅ | ❌ |
| Inline comments | ✅ | ❌ |
| Variable interpolation | ✅ | ✅ |
export prefix |
✅ | ✅ |
| Performance | ~4× faster | baseline |
Boolean Values
Performance
Benchmark results (10 000 iterations):
| Metric | laxity7/dotenv | symfony/dotenv |
|---|---|---|
| min | 0.0218 ms | 0.0773 ms |
| max | 0.3831 ms | 0.9657 ms |
| avg | 0.0238 ms | 0.0967 ms |
Run benchmarks yourself:
Error Tolerance
This .env file is valid for laxity7/dotenv, but causes exceptions in symfony/dotenv:
symfony/dotenv errors:
Development
The project uses Docker for all commands, so no local PHP installation is required.
License
MIT