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

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

gullevek/dotenv
===============

Simple .env file processing and storing in \_ENV array

v3.3.3(1mo ago)11.1k↓63.1%1MITPHPPHP &gt;=8.1.0CI passing

Since Jun 8Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/gullevek/dotEnv)[ Packagist](https://packagist.org/packages/gullevek/dotenv)[ Docs](https://github.com/gullevek/dotEnv)[ RSS](/packages/gullevek-dotenv/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (15)Versions (21)Used By (1)

dotenv: readEnvFile()
=====================

[](#dotenv-readenvfile)

A simple implementation of

This is not a functional replacement, but a very simple implementation of the basic functions.

It is recommended to create a `.env.example` example file that is checked into the repository. The `.env` should *NEVER* be checked into anything

How to install
--------------

[](#how-to-install)

`composer require gullevek/dotEnv`

Run it
------

[](#run-it)

Create a `.env` file in the current folder. Create a file like below

```
require '../vendor/autoload.php';
$status = \gullevek\dotEnv\DotEnv::readEnvFile(__DIR__);
```

All data will be in the `$_ENV` array. Note that existing `$_ENV` data is never overwritten. If this is needed make a copy fo the `$_ENV` file and reset it, or if the data is not needed just reset it.

SUCCESS with problem errors can be read from `DotEnv::getLastReadEnvFileErrors()`

### readEnvFile: Options

[](#readenvfile-options)

```
public static function readEnvFile(
    string $path = __DIR__,
    string $env_file = '.env',
    bool $load_outside_env = false,
    bool $throw_exception = false,
): DotEnvLevel
```

- `path`: Where to look for the env file, default `__DIR__`
- `env_file`: File name to load, default `.env`
- `load_outside_env`: If set to true, will first load via getenv all the names found in the env file, will return "SUCCESS\_ENV\_EXIST\_SKIP" if ther is an existing variable set already
- `throw_exception`: instead of returining DotEnvLevel will throw exceptions, see Exception

read outside environment data
-----------------------------

[](#read-outside-environment-data)

Normal environment data is not loaded into the `$_ENV` variable. If this is needed this can be done with `\gullevek\dotEnv\DotEnv::loadOutsideGetEnv()`

### loadOutsideGetEnv: Options

[](#loadoutsidegetenv-options)

```
public static function loadOutsideGetEnv(
    array $env_list = [],
    int $merge_flag = self::OVERWRITE
): void
```

- `env_list`: if set only those environment variables will be loaded, case sensitive
- `merge_flag`: defines how existing data is handled, default is to overwrite all of it
    - `OVERWRITE`: overwrite all set data in `$_ENV`
    - `MERGE_KEEP_EXISTING`: merge but keep data with the same key
    - `MERGE_OVERWRITE_EXISTING`: merge but overwrite data with the same key

Note to `env_list`, entries can be fnmatch notated

The wildcard style does not support matching with "\*" in the middle

How it works
------------

[](#how-it-works)

Put the function where it is needed or put it in a file and load it.

if not parameter is given it will use `__DIR__` as base path. Second parameter is file name override. Default is `.env`

Data is loaded into \_ENV only.

If there is already an entry in \_ENV then it will not be overwritten.

Errors
------

[](#errors)

The `readEnvFile` will return a DotEnvLevel status message

- `SUCCESS`: load successful
- `SUCCESS_DOUBLE_KEY`: load successful, but some keys where double in the file
- `SUCCESS_ENV_EXIST_SKIP`: load successful, but some keys already existed in the \_ENV array
- `SUCCESS_DOUBLE_KEY_ENV_EXIST_SKIP`: load successful, but we have double keys and some keys already existed in the \_ENV array
- `WARNING_FILE_LOADED_NO_DATA`: file exists, but no data
- `ERROR_FILE_NOT_FOUND`: file does not exist

### Reading SUCESS\_\* status block errors

[](#reading-sucess_-status-block-errors)

If we have anything other than plain SUCCESS we can check the keys that had errors with `DotEnv::getLastReadEnvFileErrors()`. The returned array key is the DotEnvLevel name as string and has a list of all loaded keys that fall into this error.

Example:

```
Array
(
    [SUCCESS_ENV_EXIST_SKIP] => Array
        (
            [0] => TEST
            [1] => FOOBAR
        )
)
```

Exceptions
----------

[](#exceptions)

Optional the method can be set to throw exceptions for file reading issuses by setting "throw\_exceptions" to true

```
require '../vendor/autoload.php';

use gullevek\dotEnv\Exceptions;

try {
    $status = gullevek\dotEnv\DotEnv::readEnvFile(throw_exceptions: true);
} catch (Exception\DotEnvFileOpenFailedException $e) {
    // file not found/not readable/open failed
}
```

.env file example
-----------------

[](#env-file-example)

A valid entry has to start with an alphanumeric string, underscores are allowed and then have an equal sign (=). After the equal sign the data block starts. Data can be quoted with double quotes (") and if this is done can stretch over multiple lines. The openeing double quote must be on the same lign as the requal sign (=). If double quoted (") charcters are used it will read each line until another double quote (") character is found. Everything after that is ignored.

Any spaces before the variable or before and after the equal sign (=) are ignored.

Line is read until `PHP_EOL`. So any trailing spaces are read too.

Any line that is not valid is ignored.

```
# this line is ignored
SOMETHING=A
OTHER="A B C"
MULTI_LINE="1 2 3
4 5 6
7 8 9" ; and this is ignored
ESCAPE="String \" inside \" other "
DOUBLE="I will be used"
DOUBLE="This will be ignored"
```

A prefix name can be set with `[PrefixName]`. Tne name rules are like for variables, but spaces are allowed, but will be converted to "\_". The prefix is valid from the time set until the next prefix block appears or the file ends.

Example

```
FOO="bar"
FOOBAR="bar bar"
[SecitonA]
FOO="other bar"
FOOBAR="other bar bar"
```

Will have environmen variables as

```
$_ENV["FOO"];
$_ENV["FOOBAR"];
$_ENV["SecitonA.FOO"];
$_ENV["SecitonA.FOOBAR"];
```

Development
-----------

[](#development)

### Phan

[](#phan)

`vendor/bin/phan --analyze-twice`

### PHPstan

[](#phpstan)

`vendor/bin/phpstan`

### PHPUnit

[](#phpunit)

Unit tests have to be run from base folder with

`vendor/bin/phpunit`

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance90

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity66

Established project with proven stability

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

Recently: every ~6 days

Total

19

Last Release

50d ago

Major Versions

v2.2.0 → v3.0.02026-03-26

PHP version history (2 changes)v2.0.0PHP &gt;=7.4.0

v3.3.2PHP &gt;=8.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/6cfbe4e76259d200068a2bddced07a1b88e377c7d81f80898fbe5b8077e6bf3c?d=identicon)[gullevek](/maintainers/gullevek)

---

Tags

envdotenvenvironment variables

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  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)[dotenv-org/phpdotenv-vault

Load environment variables from encrypted .env.vault files

1019.6k2](/packages/dotenv-org-phpdotenv-vault)

PHPackages © 2026

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