Try using phpdotenv
Sep 4, 2020
PHP
Composer
phpenv
environment
First book: 2020/09/04 PC: macOS 10.15.6 php 7.4.9 Composer: v1.10.10 phpdotenv: v5.1
What is # phpdotenv? An environment variable prepared in the .env file that can be easily handled. https://github.com/vlucas/phpdotenv
#Install
Do the following in your terminal
% composer require vlucas / phpdotenv
#Preparing the env file
Prepare a .env
file in an appropriate location and describe the environment variables you want to set.
# Some ID and password
ID = "aa"
PASS = "password"
In the env file, as mentioned above, the basic way of writing is key = “value”. You can also comment out with #.
Load with #php
<? php
declare (strict_types = 1);
// If the file name is not .env, put the file name in the second argument of createImmutable
Dotenv \ Dotenv :: createImmutable (__DIR__)-> load ();
echo $ _ENV ["ID"]; // aa
Dotenv \ Dotenv :: createImmutable (\ _ \ _DIR \ _ \ _)-> load (); will load the contents of .env into the $ _ENV variable. \ _ \ _DIR \ _ \ _ is when .env and the folder to be read are the same. If you put it in a different place, describe the place in a timely manner
When I looked it up, there was a site that used Dotenv :: create or new Dotenv, but when using declare, it was said that there were not enough arguments & the type was different, so I could not execute it.
All you have to do is use $ _ENV to get it.
Note
It’s not a note of phpdotenv, though. .. Since the .env file itself can be accessed normally and can be read because it is in text format, it is necessary to control access with htaccess etc. in the production environment.
#Reference site About phpdotenv. phpdotenv –Lara Japan