Within PHP, *.ini files can be read by using parse_ini_file()
. However, various frameworks (Laravel included) opt to, instead, bring in a separate library to parse an environment file.
What is the reasoning behind using this "dotenv" solution instead of ini files and the built-in PHP function?
An . env file is a plain text file which contains environment variables definitions which are designed so your PHP application will parse them, bypassing the Apache, NGINX and PHP-FPM. The usage of . env files is popular in many PHP frameworks such as Laravel which has built-in support for parsing .
PHP dotenv has built in validation functionality, including for enforcing the presence of an environment variable. This is particularly useful to let people know any explicit required variables that your app will not work without.
Laravel's default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then retrieved from various Laravel configuration files within the config directory using Laravel's env function.
Once you have opened the folder, click on the Explorer icon on the top left corner of the VSCode (or press Ctrl+Shift+E) to open the explorer panel. In the explorer panel, click on the New File button as shown in the following screenshot: Then simply type in the new file name . env ...
It worked for me using Laravel .env
<?php
var_dump(parse_ini_file('.env', false, INI_SCANNER_RAW));
It's a good question. I've found a few mentions on php.net (search by the keyword parse_ini_file
). The main problem, I suppose, is that parse_ini_file
doesn't support some features, such as constants, expressions, etc. Also, I guess, some developers would like to perform such operations in OOP style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With