Skip to content

markhadjar/env

Repository files navigation

Env

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.

About

A static utility for reading environment variables.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Contributors

Languages