php-standard-library/env
Tiny PHP utility for reading environment variables with sensible defaults and type casting. Helps centralize access to config via env(), supports required keys, fallback values, and safe handling when variables are missing or empty.
.env file usage and getenv() patterns.bool, int) for feature toggles, timeouts, and API keys to eliminate runtime errors from malformed inputs (e.g., "true" vs. true).env('DATABASE_URL', 'sqlite:///:memory:')).API_KEY, DB_PASSWORD) with a single, auditable API, reducing hardcoded secrets and improving compliance with security policies.env('LOG_LEVEL', env('APP_ENV') === 'local' ? 'debug' : 'error')) without bloated configuration files.getenv() usage in monolithic PHP applications with a maintainable, typed API, accelerating migration to modern practices.env() helpers for high-throughput systems (e.g., real-time APIs, cron jobs) where caching isn’t needed.Adopt if:
getenv(), $_ENV, or framework-specific helpers).symfony/validator), especially for booleans, integers, or enums.getenv() is overused, and you want a low-risk, drop-in replacement with modern features.Look elsewhere if:
vlucas/phpdotenv + symfony/validator.spatie/laravel-encryption or HashiCorp Vault.env() helpers + .env files.php-option/env or custom solutions.config() system.For Executives:
*"This package eliminates configuration chaos in our PHP services by standardizing how we handle environment variables. Right now, we’re mixing raw getenv() calls, framework-specific helpers, and inconsistent defaults—leading to bugs, CI/CD failures, and security risks. By adopting this lightweight solution, we’ll:
getenv() with a typed, maintainable API.For Engineering Teams:
*"This is a drop-in replacement for getenv()/$_ENV that gives us:
bool, int, or string values at parse time (e.g., env('DEBUG', false) always returns a boolean).env('DATABASE_URL', 'sqlite:///:memory:')) prevent runtime crashes.getenv('KEY') with env('KEY') in stages. Works alongside existing .env files. Let’s prototype it in one service (e.g., the API gateway) to validate the win."*For Developers: *"Tired of:
if (getenv('FEATURE_X') === 'true') { ... } // What if it's '1'? 'yes'? NULL?
Now you get:
if (env('FEATURE_X', false)) { ... } // Always a boolean.
composer require php-standard-library/env and go.
Start with one class (e.g., ConfigService) and expand as needed. Bonus: IDE autocompletion for env vars!"*How can I help you explore Laravel packages today?