Simple library to consume environment variables
Installs: 15 094
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 6
pkg:composer/deinternetjongens/env
Requires
- php: >=5.2
This package is not auto-updated.
Last update: 2025-10-28 18:00:27 UTC
README
Simple library to get environment variables converted to simple types.
Installation
This package is installable and autoloadable via Composer as oscarotero/env.
$ composer require deinternetjongens/env
Example
// Using getenv function: var_dump(getenv('FOO')); //string(5) "false" // Using Env: var_dump(Env::get('FOO')); //bool(false)
Available conversions:
- "false" is converted to boolean
false - "true" is converted to boolean
true - "null" is converted to
null - If the string contains only numbers is converted to an integer
- If the string has quotes, remove them
To configure the conversion, you can use the following constants (all enabled by default):
Env::CONVERT_BOOLTo convert boolean valuesEnv::CONVERT_NULLTo convert null valuesEnv::CONVERT_INTTo convert integer valuesEnv::STRIP_QUOTESTo remove the quotes of the strings
//Convert booleans and null, but not integers or strip quotes Env::$options = Env::CONVERT_BOOL | Env::CONVERT_NULL;
Default value
By default, if the value does not exits, returns null, but you can change for any other value:
Env::$default = false;
The env() function
If you don't want to complicate with classes and namespaces, you can use the env() function, like in Laravel or other libraries:
Env::init(); //expose the function to globals //now you can use it var_dump(env('FOO'));