vlucas / phpdotenv

Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
BSD 3-Clause "New" or "Revised" License
13.17k stars 629 forks source link

What's the difference? #462

Closed esjdev closed 4 years ago

esjdev commented 4 years ago

What is the difference between using phpdotenv and the code below?

function _env($name)  {
    $envPath = realpath(dirname(__FILE__) . '/.env');
    $env = parse_ini_file($envPath);

    return $env[$name];
}

echo _env('APP_URL');
GrahamCampbell commented 4 years ago

That code will be very slow since it is loading the file over and over. Also, ini files are not the same as env files, so you won't be able to use the same syntax. Moreover, other 3rd party code looking for env variables in $_SERVER won't work. For example, the AWS SDK looking for credentials, or the Bugsnag notifier.

esjdev commented 4 years ago

That code will be very slow since it is loading the file over and over. Also, ini files are not the same as env files, so you won't be able to use the same syntax. Moreover, other 3rd party code looking for env variables in $_SERVER won't work. For example, the AWS SDK looking for credentials, or the Bugsnag notifier.

Thank you very much for taking this question :D