DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Managing Environment Variables in PHP

Managing Environment Variables in PHP

The overall idea is that your code can read variables from a local text file rather than from the actual environment variables. Read on to learn more!

Lorna Mitchell user avatar by
Lorna Mitchell
·
Oct. 18, 18 · Tutorial
Like (3)
Save
Tweet
Share
12.08K Views

Join the DZone community and get the full member experience.

Join For Free

Now I work with more programming languages, I start to miss features from other languages when I come "home" to PHP. One that I hadn't seen in PHP before I saw it in other languages such as Node.js (I think Ruby had the original implementation) was: a way to easily control setting your environment variables, particularly in development. In Node.js, the dotenv library is great for this; handily in PHP vlucas has already created phpdotenv so we are all set to apply these tricks to PHP applications!

Managing Environment Variables

The overall idea is that your code can read variables from a local text file rather than from the actual environment variables. This makes it easier to switch projects because you can write a file per project and not have to re-export environment variables (or wonder why the variables are wrong if you didn't start a new shell when switching projects). Also since I often write code for other people to use, such as example applications or starter kits, the .env file provides a clear list of the values they need to set for the thing to work.

.env Config File

Usually, I use two files:

  • .env is the file where the actual values will be read from. Put the file name into .gitignore so that you never commit your actual credentials/config to the git repository!
  • .env-example hold just the keys, not the values, that a user needs to set up the project. This makes an easy "copy this file and fill in the blanks" exercise to get things rolling.

Note that you probably need some switches for production where it actually reads the environment variables that the CI system sets, since you won't be deploying a .env.

The .env file looks like this:

AWESOME_API_KEY=abc123 
AWESOME_API_SECRET=abcdef0123456789 

Once the values are there, you can teach your PHP application to use them.

PHPdotenv

First, we'll need to add the phpdotenv package to our project, using Composer:

composer require vlucas/phpdotenv 

Then, in your code add something like this to the top of index.php or wherever you do your setup (after require "vendor/autoload.php" is probably a good place!):

$dotenv = new DotenvDotenv(__DIR__);
$dotenv->load();

Then all your variables are available where PHP expects them, such as $_ENV['AWESOME_API_KEY'].

Using environment variables in development is as easy as using config files but is more in touch with the way we usually configure applications on the production environment. Environment variables are a very standard way to pass in context to an application - hence the name! They literally let the application know about the environment it is currently operating in.

Hopefully, you might find this useful for your own applications — thanks Vance for building this excellent library!

PHP

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Future of Cloud Engineering Evolves
  • Java Development Trends 2023
  • Using the PostgreSQL Pager With MariaDB Xpand
  • Spring Cloud: How To Deal With Microservice Configuration (Part 1)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: