Skip to content

Instantly share code, notes, and snippets.

@a-h-abid
Created June 16, 2019 10:10
Show Gist options
  • Save a-h-abid/e9cd7c5168f18f5a7917cd91ee2e6127 to your computer and use it in GitHub Desktop.
Save a-h-abid/e9cd7c5168f18f5a7917cd91ee2e6127 to your computer and use it in GitHub Desktop.
Enhanced ENV function mixing both Laravel & CakePHP's env()
<?php
if (!function_exists('eenv')) {
/**
* Enhanced ENV()
*
* Gets the value of an environment variable.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
function eenv($key, $default = null)
{
$value = null;
if (isset($_SERVER[$key])) {
$value = $_SERVER[$key];
} elseif (isset($_ENV[$key])) {
$value = $_ENV[$key];
} elseif (getenv($key) !== false) {
$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 (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') {
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