A static utility for reading environment variables.

Maintainers

Package info

github.com/markhadjar/env

pkg:composer/markhadjar/env

Fund package maintenance!

Buy Me A Coffee

Statistics

Installs: 55

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-05-03 01:21 UTC

This package is auto-updated.

Last update: 2026-05-03 01:29:43 UTC


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.