loading...
markdown guide
 

Yes, there is! Some frameworks like Laravel and Symfony have .env file support out of the box.

In your project, you could use vlucas/phpdotenv, which does exactly what you want and it's pretty simple to implement, check it out:

1 - Install it using Composer by typing the following command on your terminal:

composer require vlucas/phpdotenv

2 - Now let's configure it on your application's starting point (I'll assume it is index.php):

<?php
// index.php

include 'vendor/autoload.php';

$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

// your code goes here...

3 - Cool, we're almost there! Now create the .env file at your project's root directory and populate it with your configs like this:

DB_USER="user"
DB_PASSWORD="ultraSecurePassword"
DB_NAME="awesome_db"
FOO="bar"

4 - Finally, to retrieve the env values:

<?php
// db.php (naming things is hard)

$user = env('DB_USER');
// OR
$user = getenv('DB_USER');
// OR
$user = $_ENV['DB_USER'];
// OR EVEN
$user = $_SERVER['DB_USER'];

Hope it helps! ;)
(if there's any typo, feel free to correct me, i'll appreciate)

 

Awesome! Everything I was looking for!!! Thanks a bunch

 

.htconfig.ini

[MySQL]
dsn = "mysql:host=localhost;dbname=myDatabase"
user = "username"
password = "Password"

index.php

<?php
$config = parse_ini_file('.htconfig.ini' , true);
$db = new MyPDO($config['MySQL']);
 

Wow! thanks, I looked into this. It looks better than using normal dot files since there is a built in function to read ini files. Thanks a lot

 

Don't forget add to .gitignore(if you use git) this dot files !

 
Classic DEV Post from Jun 23 '19

Being a Female Programmer: How is it For You?

This probably depends on where we live and work, but personally, I have not experienced anything negative for being a female programmer in my few years of career.

Ashok Mohanakumar profile image
INTP | Full Stack Dev