sirix / sirix-env
A helper function to retrieve environment variables with type casting
Fund package maintenance!
sirix777
buymeacoffee.com/sirix
Installs: 24
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/sirix/sirix-env
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8
- devster/ubench: ^2.1
- phpunit/phpunit: ^11.5
- psr/simple-cache: ^2.0
- roave/security-advisories: dev-master
- symfony/var-dumper: ^6.3
README
A helper function to retrieve environment variables with type casting.
Installation
Install this tool using composer.
composer require sirix/sirix-env
Usage
This library provides a global env function that allows you to retrieve environment variables and automatically cast them to their appropriate PHP types.
Basic Usage
use function Sirix\Env\env; $value = env('MY_VAR'); $defaultValue = env('NON_EXISTENT', 'default');
Type Casting
The env function automatically casts string values to the following types:
| Value | PHP Type | Result |
|---|---|---|
'true', '(true)' |
boolean | true |
'false', '(false)' |
boolean | false |
'null', '(null)' |
null | null |
'empty', '(empty)' |
string | '' |
'123', '1.23' |
integer / float | 123 / 1.23 |
| Other values | string | Original string (trimmed) |
Static usage
If you prefer not to use the global function, you can use the EnvParser class directly:
use Sirix\Env\EnvParser; $value = EnvParser::parse('true'); // true