Skip to content

Instantly share code, notes, and snippets.

@robertopc
Last active May 2, 2023 14:53
Show Gist options
  • Save robertopc/b0548545611459db08ee6bbe20088bfe to your computer and use it in GitHub Desktop.
Save robertopc/b0548545611459db08ee6bbe20088bfe to your computer and use it in GitHub Desktop.
Read .env files in PHP
<?php
$consts = parse_ini_file('.env');
foreach($consts as $k => $i){
if(!defined($k))
define($k, $i);
}
@robertopc
Copy link
Author

This code can read a .env like the sample and create the constants for use in PHP

ENVIRONMENT=production
MYSQL_USER=usertest
MYSQL_PASS=123456
POSTGRE_USER=usertest
POSTGRE_PASS=123456

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