10

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?

miken32
  • 42,008
  • 16
  • 111
  • 154
Lijiebin
  • 113
  • 7

3 Answers3

4

You can parse a .env file natively in PHP using parse_ini_file. This worked for me using Laravel .env

<?php
var_dump(parse_ini_file('.env', false, INI_SCANNER_RAW));
ruleboy21
  • 5,510
  • 4
  • 17
  • 34
2

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.

Sergey Chizhik
  • 617
  • 11
  • 22
  • I really appreciate of you! Also, ini parser has some disadvantage, however there are many advantages, right? So, i still understand why dotenv, hehe. : ) – Lijiebin Dec 15 '15 at 11:50
1

We can access .env variables in many ways, but it is not a benefit for single project. It is better to use unified way to access these variables throughout the project any way. However, laravel is used for many projects and opting for flexibility is understandable for them.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191