A static utility for reading environment variables.
PHP 8.4+
Install via Composer:
composer require markhadjar/envThe Env class provides static methods for retrieving values from $_ENV.
Use Env::get() to retrieve raw values. If the key is not set or its value is null, the default is returned:
use MarkHadjar\Env\Env;
$raw = Env::get('RAW');
$rawWithDefault = Env::get('RAW', 'fallback');Use the typed getters (getBool, getInt, getFloat, getString) to retrieve values as a specific type.
use MarkHadjar\Env\Env;
// Accepts only native booleans and the strings 'true' and 'false' (case-insensitive)
$bool = Env::getBool('BOOL');
$boolWithDefault = Env::getBool('BOOL', false);
// Accepts native integers and integer strings
$int = Env::getInt('INT');
$intWithDefault = Env::getInt('INT', 1);
// Accepts native floats and numeric strings
$float = Env::getFloat('FLOAT');
$floatWithDefault = Env::getFloat('FLOAT', 1.5);
// Accepts native strings only
$string = Env::getString('STRING');
$stringWithDefault = Env::getString('STRING', 'abc');Note: Typed getters return null or the provided default when the key is not set or its value is null. If the key is present but the value cannot be cast to the requested type, a ValueCouldNotBeCast exception is thrown.