Skip to content

Instantly share code, notes, and snippets.

@felixdorn
Created November 30, 2019 11:25
Show Gist options
  • Save felixdorn/7db21ed76229cfd188baa700e2cd8982 to your computer and use it in GitHub Desktop.
Save felixdorn/7db21ed76229cfd188baa700e2cd8982 to your computer and use it in GitHub Desktop.
/**
* @param string $key
* @param string|null $default
* @return string|boolean
*/
function env(string $key, string $default = null)
{
$getKey = static function ($key, $default) {
if (notEmpty(array_get($_SERVER, $key))) {
return $_SERVER[$key];
}
if (notEmpty(array_get($_ENV, $key))) {
return $_ENV[$key];
}
if (notEmpty(getenv($key))) {
return getenv($key);
}
return $default ?? false;
};
$key = trim($getKey($key, $default));
if (in_array($key, ['yes', 'true', 'TRUE'])) {
return true;
}
if (in_array($key, ['no', 'false', 'FALSE'])) {
return false;
}
if (is_numeric($key)) {
return (int)$key;
}
return $key;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment