PHPackages                             jchook/dotenv-to-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jchook/dotenv-to-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jchook/dotenv-to-php
====================

Convert dotenv files to PHP

v1.0.3(8y ago)32.7kMITShellPHP &gt;=5.3.0

Since Apr 29Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jchook/dotenv-to-php)[ Packagist](https://packagist.org/packages/jchook/dotenv-to-php)[ Docs](https://github.com/jchook/dotenv-to-php)[ RSS](/packages/jchook-dotenv-to-php/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)DependenciesVersions (5)Used By (0)

dotenv-to-php
=============

[](#dotenv-to-php)

Convert dotenv files to PHP.

[![Demo of dotenv-to-php](https://camo.githubusercontent.com/4ec0829d86c74687d125050a775d43f7edb24fe4c4b6f2b9d29e8d8b3f64602f/68747470733a2f2f692e696d6775722e636f6d2f4f4a546f4b31762e676966)](https://camo.githubusercontent.com/4ec0829d86c74687d125050a775d43f7edb24fe4c4b6f2b9d29e8d8b3f64602f/68747470733a2f2f692e696d6775722e636f6d2f4f4a546f4b31762e676966)

Why use it?
-----------

[](#why-use-it)

Storing configuration in the environment is [a tenant of a Twelve Factor App](https://www.12factor.net/config) and has many advantages. For example, it allows you to keep your passwords separate from your code.

However, it's not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. In these scenarios, we can load variables from a .env as needed.

Instead of parsing a .env at runtime with complex PHP dependencies, we can use `dotenv-to-php` to compile our .env to native PHP.

1. **It's fast.** The conversion is instantaneous. At runtime, simply load your env vars using the generated PHP. You can even use it in production.
2. **It's simple.** No runtime dependency. The transpiler is only [42 lines with comments](bin/dotenv-to-php), contains no regex, and no custom parsers. Contrast this with [phpdotenv](https://github.com/vlucas/phpdotenv/blob/475e5e0d27d669a59f9a6d04844255fa302d5d39/src/Loader.php#L228).

1. Install
----------

[](#1-install)

You can just [download the one file](bin/dotenv-to-php) **-OR-** add it as a dev dependency via composer:

```
composer require --dev jchook/dotenv-to-php

```

2. Create .env
--------------

[](#2-create-env)

If you don't already have one, create a `.env` file in the root directory of your project. Add all environment-specific variables (anything likely to change between servers or deployments) on individual lines like so:

```
# Application
APP_HOST="myapp.com"
APP_URI="https://$APP_HOST/"
APP_NAME="My Application"

# Database
DB_HOST=localhost
DB_USER=myapp
DB_PASS=mypass
```

Notice that you should quote values with spaces, etc. and that it's possible to reference previously defined variables as in normal shell environments. Comments beginning with `#` are ignored.

**Do not commit your .env file to source control.** Instead, store it somewhere secure, or use a tool like [vault](https://www.vaultproject.io/).

3. Build .env.php
-----------------

[](#3-build-envphp)

Simply call [`bin/dotenv-to-php`](bin/dotenv-to-php) via command-line to convert your .env file to PHP.

```
/path/to/dotenv-to-php
```

Usage is roughly `dotenv-to-php [infile] [outfile]`. If no infile or outfile is specified, they default to `.env` and `$infile.php` respectively. You can also specify `-` (hyphen) for stdin or stdout... respectively.

4. Integrate
------------

[](#4-integrate)

In your PHP application, simply include the compiled `.env.php` file. Your loaded environment variables will be available via `$_SERVER`, `$_ENV`, and `getenv()`.

```

```

Ideally you can integrate the `.env.php` file generation into your existing build process (either for CI or local development). All examples below assume that your dotenv file is saved in the current working directory as `.env`.

### Composer

[](#composer)

If you installed via composer, you can integrate it as a script in your `composer.json` file:

```
{
	"scripts": {
		"build-env": "dotenv-to-php .env .env.php"
	}
}
```

Then invoke via:

```
composer build-env
```

### Node JS

[](#node-js)

Many folks are using node js to compile their static assets such as javascript. If you want to plug-in to this existing build process, you can use the built-in [child\_process](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) module.

```
const childProcess = require('child_process');
childProcess.spawn('/path/to/dotenv-to-php', ['.env', '.env.php']);
```

### Webpack 4+

[](#webpack-4)

You can easily incorporate the above into an ad-hoc webpack plugin. In your `webpack.config.js` file:

```
const childProcess = require('child_process');
module.exports = {

	// ... other config here ...

	plugins: [

		// ... other plugins here ...

		// Build PHP env
		{apply: (compiler) => {
			compiler.hooks.afterEnvironment.tap('DotEnvToPhp', (compilation) => {
				childProcess.spawn('/path/to/dotenv-to-php', ['.env', '.env.php']);
			});
		}}
	]
};
```

License
-------

[](#license)

MIT.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~18 days

Total

4

Last Release

2931d ago

PHP version history (2 changes)v1.0.0PHP &gt;=7.1.0

1.0.1PHP &gt;=5.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/047da3410a118e06b8f9e170318c1c1197a6dbd5ceebe1a43ee91251d49c7632?d=identicon)[jchook](/maintainers/jchook)

---

Top Contributors

[![jchook](https://avatars.githubusercontent.com/u/193530?v=4)](https://github.com/jchook "jchook (14 commits)")

---

Tags

convertenvdotenv

### Embed Badge

![Health badge](/badges/jchook-dotenv-to-php/health.svg)

```
[![Health](https://phpackages.com/badges/jchook-dotenv-to-php/health.svg)](https://phpackages.com/packages/jchook-dotenv-to-php)
```

###  Alternatives

[vlucas/phpdotenv

Loads environment variables from `.env` to `getenv()`, `$\_ENV` and `$\_SERVER` automagically.

13.5k640.2M6.3k](/packages/vlucas-phpdotenv)[symfony/dotenv

Registers environment variables from a .env file

3.8k243.3M2.8k](/packages/symfony-dotenv)[helhum/dotenv-connector

Makes it possible to set environment variables for composer projects.

1634.9M41](/packages/helhum-dotenv-connector)[m1/env

Env is a lightweight library bringing .env file parser compatibility to PHP. In short - it enables you to read .env files with PHP.

6412.8M24](/packages/m1-env)[sroze/companienv

Companion for .env files

246424.9k1](/packages/sroze-companienv)[bnomei/kirby3-dotenv

Kirby Plugin for environment variables from .env

4149.6k2](/packages/bnomei-kirby3-dotenv)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
