Keep database passwords and API keys out of your codebase by storing them in a .env file that never gets committed to git.
Validate that required config variables are present at startup, catching missing credentials before your app crashes with a mysterious error.
Share a .env.example file with your team so everyone knows which variables to set up without exposing real credentials.
Use variable nesting with ${} syntax to build one config value from another in your .env file.
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.
← vlucas on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.