explaingit

vlucas/phpdotenv

13,544PHPAudience · developerComplexity · 2/5Setup · easy

TLDR

PHP library that reads a .env file and loads configuration values like passwords and API keys into your app as environment variables, keeping secrets out of your source code and version control.

Mindmap

mindmap
  root((phpdotenv))
    What It Does
      Load .env file
      Set env variables
      Validate required vars
    Use Cases
      Keep secrets out of code
      Team onboarding config
      Multi-environment setup
    Tech
      PHP
      Composer install
    Features
      Variable nesting
      Multiple .env files
      Allowed variable list
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Keep database passwords and API keys out of your codebase by storing them in a .env file that never gets committed to git.

USE CASE 2

Validate that required config variables are present at startup, catching missing credentials before your app crashes with a mysterious error.

USE CASE 3

Share a .env.example file with your team so everyone knows which variables to set up without exposing real credentials.

USE CASE 4

Use variable nesting with ${} syntax to build one config value from another in your .env file.

Tech stack

PHPComposer

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

PHP dotenv is a library that reads a plain text file called .env and loads the values inside it into your PHP application as environment variables. The idea is to keep sensitive configuration, like database passwords, API keys, and secret tokens, out of your source code and out of version control. Instead of hardcoding these values, you put them in a .env file that stays on each server or developer machine privately, and the library makes those values available to your code as if they had been set by the operating system. The typical workflow is to create a .env file with real values for your current environment, then add that file to your .gitignore so it never gets committed. You also create a .env.example file with placeholder or dummy values that does get committed, so other developers know which variables they need to set up. Each person on the team fills in their own .env with their own credentials. Once you call the library's load method at the start of your application, the variables from the .env file become accessible through the standard PHP $_ENV and $_SERVER superglobal arrays. The library also supports requiring that certain variables are present and throwing a clear error if they are missing, which helps catch configuration problems early rather than getting mysterious failures later. You can check that values are not empty, match a list of allowed options, or follow a particular pattern. Other features include nesting one variable inside another using a ${} syntax, controlling whether the library overwrites variables that were already set before loading, restricting which variable names are allowed to load at all, and using multiple .env files at once. The library follows standard PHP package conventions and installs through Composer. It is modeled after the original Ruby dotenv library and has been a standard part of PHP projects for many years.

Copy-paste prompts

Prompt 1
Set up vlucas/phpdotenv in a PHP project to load database credentials from a .env file and require that DB_HOST, DB_USER, and DB_PASS are all present, throw a clear error if any are missing.
Prompt 2
Using phpdotenv, how do I prevent it from overwriting environment variables that were already set by the server before my app loads the .env file?
Prompt 3
Show me how to use phpdotenv's variable nesting feature so I can build a DATABASE_URL from separate DB_HOST and DB_PORT variables in the same .env file.
Prompt 4
Create a .env.example file for a PHP project that uses phpdotenv, with placeholders for all required variables and comments explaining each one.
Open on GitHub → Explain another repo

← vlucas on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.