Skip to content

Instantly share code, notes, and snippets.

@JPKCom
Created November 14, 2023 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JPKCom/09e308d41fa74af50d00da97243f977a to your computer and use it in GitHub Desktop.
Save JPKCom/09e308d41fa74af50d00da97243f977a to your computer and use it in GitHub Desktop.
Simple .env connector
<?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