PHP dotenv reads a .env file and populates $_ENV and $_SERVER, with optional getenv support, so apps keep secrets out of code.
Overview
PHP dotenv is a Composer package that loads environment variables from a .env file into PHP’s superglobals. It follows the same general idea as the Ruby dotenv project and is meant for keeping credentials and deployment-specific settings out of application source.
Teams typically commit a .env.example with required keys and dummy or blank values while keeping the real .env local and out of version control. Loading is done through the Dotenv API in application bootstrap code, with options for alternate filenames, multiple searchable files, merge behavior, and encodings.
The loader supports nested variable references, quoting rules, comments, and both immutable and mutable modes that control whether existing environment values may be overwritten. Optional unsafe adapters can expose variables through getenv and putenv, though the docs discourage relying on those functions for thread-safety reasons.
Built-in validation can require variables to be set, non-empty, integers, booleans, or members of allowed sets, including regex checks. A parse API can turn file contents into an array without touching the live environment, which helps testing or preprocessing.
Key features
- Loads .env into $_ENV and $_SERVER
- Immutable and mutable modes
- Nested variable interpolation
- Quoted value and escape handling
- Multi-file merge loading
- Optional getenv support
- Variable validation rules
- Parse without loading
Use cases
- Per-environment PHP configuration
- Keeping secrets out of git
- Shared .env.example onboarding
- Validating required env vars
- Apache, Nginx, or CLI PHP runs