Skip to content

Instantly share code, notes, and snippets.

@mattstauffer
Created November 22, 2015 00:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattstauffer/59170bc2cb6318ab6d46 to your computer and use it in GitHub Desktop.
Save mattstauffer/59170bc2cb6318ab6d46 to your computer and use it in GitHub Desktop.
Laravel's env() helper
<?php
/**
* Gets the value of an environment variable. Supports boolean, empty and null.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return value($default);
}
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return;
}
if (Str::startsWith($value, '"') && Str::endsWith($value, '"')) {
return substr($value, 1, -1);
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment