Created
November 14, 2023 14:17
-
-
Save JPKCom/09e308d41fa74af50d00da97243f977a to your computer and use it in GitHub Desktop.
Simple .env connector
This file contains 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( 'jpkcom_getenv' ) ) { | |
if ( file_exists( dirname(__FILE__) . '/.env' ) ) { | |
$lines = file( dirname(__FILE__) . '/.env' ); | |
foreach ( $lines as $line ) { | |
[ $key, $value ] = explode( '=', $line, 2 ); | |
$key = trim( $key ); | |
$value = trim( $value ); | |
putenv( sprintf( '%s=%s', $key, $value ) ); | |
$_ENV[ $key ] = $value; | |
$_SERVER[ $key ] = $value; | |
} | |
} | |
function jpkcom_getenv ( $env, $default ) { | |
if ( ( $val = getenv( $env ) ) !== false ) { | |
return $val; | |
} else { | |
return $default; | |
} | |
} | |
} | |
// Example: | |
echo jpkcom_getenv('TEST_ENV', 'NO .ENV'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment