PHPackages                             oohology/dotenvwriter - 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. oohology/dotenvwriter

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

oohology/dotenvwriter
=====================

Interface for editing .env files in PHP.

v1.3.1(8y ago)658.6k—0.7%1[1 issues](https://github.com/oohology/dotenvwriter/issues)1BSD-3-ClausePHPPHP &gt;=5.4.0

Since Oct 21Pushed 8y ago3 watchersCompare

[ Source](https://github.com/oohology/dotenvwriter)[ Packagist](https://packagist.org/packages/oohology/dotenvwriter)[ RSS](/packages/oohology-dotenvwriter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (1)Versions (20)Used By (1)

DotEnvWriter
============

[](#dotenvwriter)

Interface for editing .env files in PHP

[![Build Status](https://camo.githubusercontent.com/e25055dfb10b0ddff3895414ea44f29565e54b12902e2dcc12e989b55ecfa4ef/68747470733a2f2f7472617669732d63692e6f72672f6f6f686f6c6f67792f646f74656e767772697465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/oohology/dotenvwriter)

---

Note: This is probably not advisable for use in production, but could be handy for automating installation tasks, etc.

Basic Usage
-----------

[](#basic-usage)

In general, you will open a .env file, use the `set` method to append or replace some values, and then call the `save` method to write the result.

Open an environment file and replace a value:

```
use DotEnvWriter\DotEnvWriter;

$env = new DotEnvWriter('../.env');
$env->set('ENVIRONMENT', 'dev');
$env->save();

```

Also supports fluent interface:

```
(new DotEnvWriter('../.env'))
    ->set('DB_HOST', 'localhost')
    ->set('DB_NAME', 'test_db')
    ->save();

```

Output File
-----------

[](#output-file)

There are multiple ways to read from a source file, make some changes, and write the result to a different output file. The end result is the same, so choose whichever method best fits your use case:

```
$writer = (new DotEnvWriter('.env.example'))
	->setOutputPath('.env');
// ...
$writer->save();

```

Or alternately:

```
$writer = (new DotEnvWriter('.env.example'));
// ...
$writer->save('.env');

```

Or using the `load()` method:

```
$writer = (new DotEnvWriter('.env'))
	->load('.env.example');
// ...
$writer->save();

```

Comments
--------

[](#comments)

The `set()` method takes a `$comments` parameter. If omitted (or set to `null`), any existing comment from the source file with be kept intact. If a `$comment` is provided it will overwrite the existing comment. Providing a zero-length `$comment` will cause the comment to be deleted.

**Examples:**Set a comment

```
// source: API_KEY=""
$writer->set('API_KEY', '1234', 'four-digit code');
// result: API_KEY=1234 # four-digit code

```

Delete a comment

```
// source: API_KEY= # four-digit code
$writer->set('API_KEY', '1234', '');
// result: API_KEY=1234

```

Keep existing comment

```
// source: API_KEY= # four-digit code
$writer->set('API_KEY', '1234');
// result: API_KEY=1234 # four-digit code

```

Export
------

[](#export)

The parser supports a bash-style `export` prefix on any line. The `set()` method takes an `$export` variable as its 4th argument. **Example:**By default, keep the existing state

```
// source: export API_KEY=""
$writer->set('API_KEY', '1234');
// result: export API_KEY=1234

```

Or change it by passing a boolean

```
// source: export API_KEY=""
$writer->set('API_KEY', '1234', null, false);
// result: API_KEY=1234
$writer->set('API_KEY', '1234', null, true);
// result: export API_KEY=1234

```

Casting booleans
----------------

[](#casting-booleans)

By default boolean values will not be stored as `true` or `false`.

To enable casting booleans you should call the `castBooleans()` method.

**Example:**

Default behaviour

```
$writer->set('REGISTRATION_OPENED', true);
// result: REGISTRATION_OPENED=1

$writer->set('REGISTRATION_OPENED', false);
// result: REGISTRATION_OPENED=

```

After calling `castBooleans()` method

```
$writer->castBooleans();

$writer->set('REGISTRATION_OPENED', true);
// result: REGISTRATION_OPENED=true

$writer->set('REGISTRATION_OPENED', false);
// result: REGISTRATION_OPENED=false

```

Read the Value of a Variable
----------------------------

[](#read-the-value-of-a-variable)

The `get` method allows you to find the value of an existing given environment variable. It returns false if the variable doesn't exist, or an array containing the details of the line from the source file.

Source: `export ENV="dev" # dev or live?`

Get command: `$writer->get('ENV');`

Result:

```
[
    'line' => 'export ENV="dev" # dev or live?',
    'export' => 'export',
    'key' => 'ENV',
    'value' => 'dev',
    'comment' => 'dev or live?'
];

```

Writing Blank/Comment Lines
---------------------------

[](#writing-blankcomment-lines)

The `line` method appends a single unprocessed line of output to the file. It could be used to insert blank lines or comments. If you wish to append a new variable, the `set` method should be used instead to prevent duplicates.

```
$writer = (new DotEnvWriter)
    ->line()
    ->line('# App Settings')
    ->line()
    ->set('APP_ENV', 'dev');

```

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 86.1% 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 ~30 days

Recently: every ~99 days

Total

19

Last Release

2948d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3557299?v=4)[David Woodmansee](/maintainers/davidwoodmansee)[@davidwoodmansee](https://github.com/davidwoodmansee)

---

Top Contributors

[![davidwoodmansee](https://avatars.githubusercontent.com/u/3557299?v=4)](https://github.com/davidwoodmansee "davidwoodmansee (31 commits)")[![shadoWalker89](https://avatars.githubusercontent.com/u/1449151?v=4)](https://github.com/shadoWalker89 "shadoWalker89 (4 commits)")[![johnnymast](https://avatars.githubusercontent.com/u/121194?v=4)](https://github.com/johnnymast "johnnymast (1 commits)")

---

Tags

phpwriterdotenv

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/oohology-dotenvwriter/health.svg)

```
[![Health](https://phpackages.com/badges/oohology-dotenvwriter/health.svg)](https://phpackages.com/packages/oohology-dotenvwriter)
```

###  Alternatives

[josegonzalez/dotenv

dotenv file parsing for PHP

2799.8M137](/packages/josegonzalez-dotenv)[suin/php-rss-writer

Yet another simple RSS writer library for PHP 5.4 or later.

2651.4M20](/packages/suin-php-rss-writer)

PHPackages © 2026

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