Simple library to read environment variables and convert to simple types.
MIT License
Simple library to get environment variables converted to simple types.
This package is installable and autoloadable via Composer as oscarotero/env.
$ composer require oscarotero/env
use Env\Env;
// Using getenv function:
var_dump(getenv('FOO')); //string(5) "false"
// Using Env:
var_dump(Env::get('FOO')); //bool(false)
"false"
is converted to boolean false
"true"
is converted to boolean true
"null"
is converted to null
To configure the conversion, you can use the following constants (all enabled by default):
Env::CONVERT_BOOL
To convert boolean valuesEnv::CONVERT_NULL
To convert null valuesEnv::CONVERT_INT
To convert integer valuesEnv::STRIP_QUOTES
To remove the quotes of the stringsThere's also additional settings that you can enable (they're disabled by default)
Env::USE_ENV_ARRAY
To get the values from $_ENV
, instead getenv()
.Env::USE_SERVER_ARRAY
To get the values from $_SERVER
, instead getenv()
.Env::LOCAL_FIRST
To get first the values of locally-set environment variables.use Env\Env;
//Convert booleans and null, but not integers or strip quotes
Env::$options = Env::CONVERT_BOOL | Env::CONVERT_NULL;
//Add one more option
Env::$options |= Env::USE_ENV_ARRAY;
//Remove one option
Env::$options ^= Env::CONVERT_NULL;
By default, if the value does not exist, returns null
, but you can change for any other value:
use Env\Env;
Env::$default = false;
You can use the env()
function, like in Laravel or other frameworks:
use function Env\env;
var_dump(env('FOO'));
Please see CHANGELOG for more information about recent changes.
The MIT License (MIT). Please see LICENSE for more information.