whatisgithub

What is phpdotenv?

vlucas/phpdotenv — explained in plain English

Analysis updated 2026-06-24

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

In one sentence

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

What do people build with it?

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.

What is it built with?

PHPComposer

How does it compare?

vlucas/phpdotenvthephpleague/flysystemphp-cs-fixer/php-cs-fixer
Stars13,54413,56513,512
LanguagePHPPHPPHP
Setup difficultyeasyeasyeasy
Complexity2/52/52/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min

So what is it?

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.

Frequently asked questions

What is phpdotenv?

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.

What language is phpdotenv written in?

Mainly PHP. The stack also includes PHP, Composer.

How hard is phpdotenv to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is phpdotenv for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.