Created
June 16, 2019 10:10
-
-
Save a-h-abid/e9cd7c5168f18f5a7917cd91ee2e6127 to your computer and use it in GitHub Desktop.
Enhanced ENV function mixing both Laravel & CakePHP's env()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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