PHPackages                             ctorh23/dotenv - 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. ctorh23/dotenv

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

ctorh23/dotenv
==============

Loads environment variable definitions from a file into the $\_ENV superglobal.

v1.1.0(2mo ago)09MITPHPPHP ^8.3CI passing

Since Feb 28Pushed 2mo agoCompare

[ Source](https://github.com/ctorh23/dotenv)[ Packagist](https://packagist.org/packages/ctorh23/dotenv)[ RSS](/packages/ctorh23-dotenv/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)DependenciesVersions (3)Used By (0)

Dotenv - Environment Variables Manager
======================================

[](#dotenv---environment-variables-manager)

**Dotenv** parses files containing user-defined environment variables and populates the $\_ENV superglobal with the resulting values.

Installation
------------

[](#installation)

**Dotenv** requires **PHP 8.3** or newer.

`composer require ctorh23/dotenv`

Usage
-----

[](#usage)

### Basic Example

[](#basic-example)

```
use Ctorh23\Dotenv\Dotenv;

$dotenv = new Dotenv(__DIR__);
$dotenv->load();
```

In the code snippet above, you provide a directory containing your *.env* file. It may also contain *.env.local*, *.env-environment\_specific*, and *.env-environment\_specific.local* files. These will be parsed in the following order:

1. *.env*
2. *.env.local*
3. *.env-environment\_specific*
4. *.env-environment\_specific.local*

If the same variable is declared in multiple files, the last one will overwrite its definition in the previous files. *environment\_specific* could be, for example, *'development'*, *'testing'*, *'production'*, etc. Which *environment\_specific* file will be parsed depends on the special variable `APP_ENV`, which must be declared in either *.env* or *.env.local*. You can change the name of this variable:

```
$dotenv->setAppEnvName('APPLICATION_ENVIRONMENT');
```

By default, **Dotenv** doesn't overwrite existing environment variables. This includes variables defined in the shell or by the web server. This means that if `EXAMPLE_VAR=example-value` has already been defined in the shell, it will not be overwritten, even if it is defined in one of the *.env* files. This behavior can be changed:

```
$dotenv->setOverwrite(true);
```

You can also provide the path to the *.env* files using the `setPath` method instead of a constructor argument:

```
$dotenv->setPath(__DIR__);
```

If your file is not named *.env*, but instead *my\_vars*, for example, you can provide a path to that file:

```
$dotenv = new Dotenv();
$dotenv->setPath(__DIR__ . '/my_vars')
    ->load();
```

Then **Dotenv** will parse the following files:

1. *my\_vars*
2. *my\_vars.local*
3. *my\_vars-environment\_specific*
4. *my\_vars-environment\_specific.local*

Finally, you can use environment variables in your application by calling the `getVar` static method:

```
$myVar = Dotenv::getVar('myvar');
$anotherVar = Dotenv::getVar('non-existent', 'default_value'); //The second argument is a default value in case a variable does not exist.
```

### Alternative Usage

[](#alternative-usage)

You can choose not to use the default logic. You can provide a single file to the **Dotenv** object:

```
$dotenv = new Dotenv();
$vars = $dotenv->processFile(__DIR__ . '/custom-vars');
```

Or you can provide a list of files:

```
$dotenv = new Dotenv();
$vars = $dotenv->processFileList([
    __DIR__ . '/custom-vars',
    __DIR__ . '/custom-vars.development',
]);
```

Then, you have to load the parsed variables into the `$_ENV` superglobal:

```
$dotenv->writeVars($vars);
```

Syntax of .env Files
--------------------

[](#syntax-of-env-files)

Environment variable definitions must follow this format:

```
VAR=value

# This is a comment
ANOTHER_VAR="2nd value"

```

- The name of a variable must begin with an alphabetic character or an underscore, followed by alphanumeric characters or underscores.
- Any character is accepted for the value, but escape sequences ('\\n', '\\t', '\\r', etc.) must be preceded by a backslash.
- Lines starting with `#` are ignored and not parsed.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance87

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 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 ~15 days

Total

2

Last Release

64d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ccefe0861d49c33342e93790a711d8bbad79cf04560b0c4e03888e879345f81f?d=identicon)[ctorh23](/maintainers/ctorh23)

---

Top Contributors

[![ctorh23](https://avatars.githubusercontent.com/u/54403634?v=4)](https://github.com/ctorh23 "ctorh23 (5 commits)")

### Embed Badge

![Health badge](/badges/ctorh23-dotenv/health.svg)

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

###  Alternatives

[spatie/laravel-mix-preload

Add preload and prefetch links based your Mix manifest

169176.0k2](/packages/spatie-laravel-mix-preload)[wcm/git-php-hooks

Write git hooks with PHP, organize them on a per-project base and automatically add them

6441.1k3](/packages/wcm-git-php-hooks)

PHPackages © 2026

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