markhadjar / env
A static utility for reading environment variables.
Fund package maintenance!
v1.0.0
2026-05-03 01:21 UTC
Requires
- php: ^8.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^13.0
README
A static utility for reading environment variables.
Requirements
PHP 8.4+
Installation
Install via Composer:
composer require markhadjar/env
Usage
The Env class provides static methods for retrieving values from $_ENV.
Reading Raw Values
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');
Reading Typed Values
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.