Skip to content

Instantly share code, notes, and snippets.

@kdssoftware
Created July 2, 2021 07:37
Show Gist options
  • Save kdssoftware/5064473a8941fe92bad887eac14c6e22 to your computer and use it in GitHub Desktop.
Save kdssoftware/5064473a8941fe92bad887eac14c6e22 to your computer and use it in GitHub Desktop.
loads .env file into $_ENV
<?php
class env
{
//usage:
//env::$load() // loads .env file
//env::$load("dev") //loads dev.env file
//CAUTION! : at an empty line after the last variable entry. see example below
public static function load ($prefix="") {
$file = file($_SERVER['DOCUMENT_ROOT'].'/ici/'.$prefix.'.env');
foreach ($file as $line_num => $line) {
$ar = explode('=',$line);
if(count($ar)==2) {
$_ENV[$ar[0]] = substr($ar[1], 0, -2);
}
}
}
}
//start .env example:
//foo=bar
//first=1
//
//end .env example
@bunnyhead007
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment