PHPackages                             zero-to-prod/phpdotenv - 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. zero-to-prod/phpdotenv

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

zero-to-prod/phpdotenv
======================

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

v0.0.3(5mo ago)01.8k[1 PRs](https://github.com/zero-to-prod/phpdotenv/pulls)1MITPHPPHP &gt;=7.1CI passing

Since Nov 29Pushed 2mo agoCompare

[ Source](https://github.com/zero-to-prod/phpdotenv)[ Packagist](https://packagist.org/packages/zero-to-prod/phpdotenv)[ Docs](https://github.com/zero-to-prod/phpdotenv)[ Fund](https://github.com/sponsors/zero-to-prod)[ RSS](/packages/zero-to-prod-phpdotenv/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (4)Used By (1)

Zerotoprod\\Phpdotenv
=====================

[](#zerotoprodphpdotenv)

[![](art/logo.png)](art/logo.png)

[![Repo](https://camo.githubusercontent.com/9a90a3efeee26aed7d7f2feee9cd84566a26f9c362cc773b184d076210906e1c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6769746875622d677261793f6c6f676f3d676974687562)](https://github.com/zero-to-prod/phpdotenv)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/43ba8fcc50ef1e03d931a534f9fd603bc1a1f2097227f280b1c26c3b9f88d8ec/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a65726f2d746f2d70726f642f706870646f74656e762f746573742e796d6c3f6c6162656c3d74657374)](https://github.com/zero-to-prod/phpdotenv/actions)[![GitHub Actions Workflow Status](https://camo.githubusercontent.com/9d215b69bf6a5a1870b27057d84a1a9c151420ca8e91975aec6794334cc65e60/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7a65726f2d746f2d70726f642f706870646f74656e762f6261636b77617264735f636f6d7061746962696c6974792e796d6c3f6c6162656c3d6261636b77617264735f636f6d7061746962696c697479)](https://github.com/zero-to-prod/phpdotenv/actions)[![Packagist Downloads](https://camo.githubusercontent.com/2fc7a8827bdc02f613e0aac628611b3c7c2a4eb3eb12252c43ad4d2c1f831f12/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7a65726f2d746f2d70726f642f706870646f74656e763f636f6c6f723d626c7565)](https://packagist.org/packages/zero-to-prod/phpdotenv/stats)[![php](https://camo.githubusercontent.com/0f0820eb158d4d543692ba9047b335e9bf70c4e164221cf10cbae5af97b1ae8c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f7a65726f2d746f2d70726f642f706870646f74656e762e7376673f636f6c6f723d707572706c65)](https://packagist.org/packages/zero-to-prod/phpdotenv/stats)[![Packagist Version](https://camo.githubusercontent.com/73834162abb33e88ae1187c6bbaac68b69388693d0e3606d19facf08663615c1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a65726f2d746f2d70726f642f706870646f74656e763f636f6c6f723d663238643161)](https://packagist.org/packages/zero-to-prod/phpdotenv)[![License](https://camo.githubusercontent.com/9d8390cabcda13a130d96e1f039f0b79087c769ff01997f699da452c0ac5942f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7a65726f2d746f2d70726f642f706870646f74656e763f636f6c6f723d70696e6b)](https://github.com/zero-to-prod/phpdotenv/blob/main/LICENSE.md)[![wakatime](https://camo.githubusercontent.com/73fd524ac8c58b79765f7c2022d1cfa0e98315a32d4715c305c8a5e8b8fadfd8/68747470733a2f2f77616b6174696d652e636f6d2f62616467652f6769746875622f7a65726f2d746f2d70726f642f706870646f74656e762e737667)](https://wakatime.com/badge/github/zero-to-prod/phpdotenv)[![Hits-of-Code](https://camo.githubusercontent.com/86d209be2cf6620a44be836ff55ed41f3f2d25d06f8c3cd532043788a02cea22/68747470733a2f2f686974736f66636f64652e636f6d2f6769746875622f7a65726f2d746f2d70726f642f706870646f74656e763f6272616e63683d6d61696e)](https://hitsofcode.com/github/zero-to-prod/phpdotenv/view?branch=main)

Contents
--------

[](#contents)

- [Introduction](#introduction)
- [Requirements](#requirements)
- [Installation](#installation)
- [Documentation Publishing](#documentation-publishing)
    - [Automatic Documentation Publishing](#automatic-documentation-publishing)
- [Usage](#usage)
- [Local Development](./LOCAL_DEVELOPMENT.md)
- [Contributing](#contributing)

Introduction
------------

[](#introduction)

Loads environment variables from `.env` to `getenv()`, `$_ENV`

Requirements
------------

[](#requirements)

- PHP 7.1 or higher.

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

[](#installation)

Install `Zerotoprod\Phpdotenv` via [Composer](https://getcomposer.org/):

```
composer require zero-to-prod/phpdotenv
```

This will add the package to your project’s dependencies and create an autoloader entry for it.

Documentation Publishing
------------------------

[](#documentation-publishing)

You can publish this README to your local documentation directory.

This can be useful for providing documentation for AI agents.

This can be done using the included script:

```
# Publish to default location (./docs/zero-to-prod/phpdotenv)
vendor/bin/zero-to-prod-phpdotenv

# Publish to custom directory
vendor/bin/zero-to-prod-phpdotenv /path/to/your/docs
```

#### Automatic Documentation Publishing

[](#automatic-documentation-publishing)

You can automatically publish documentation by adding the following to your `composer.json`:

```
{
  "scripts": {
    "post-install-cmd": [
      "zero-to-prod-phpdotenv"
    ],
    "post-update-cmd": [
      "zero-to-prod-phpdotenv"
    ]
  }
}
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

The `Phpdotenv` class provides two static methods for parsing environment variables:

#### 1. Parse from String

[](#1-parse-from-string)

Parse environment variables directly from a string:

```
use Zerotoprod\Phpdotenv\Phpdotenv;

$env_content = "APP_NAME=MyApp\nAPP_ENV=production\nAPP_DEBUG=false";
$variables = Phpdotenv::parseFromString($env_content);

// Result:
// [
//     'APP_NAME' => 'MyApp',
//     'APP_ENV' => 'production',
//     'APP_DEBUG' => 'false'
// ]
```

#### 2. Parse from Array

[](#2-parse-from-array)

Parse environment variables from an array of lines:

```
use Zerotoprod\Phpdotenv\Phpdotenv;

$lines = [
    'APP_NAME=MyApp',
    'APP_ENV=production',
    'APP_DEBUG=false'
];
$variables = Phpdotenv::parse($lines);

// Result:
// [
//     'APP_NAME' => 'MyApp',
//     'APP_ENV' => 'production',
//     'APP_DEBUG' => 'false'
// ]
```

### Loading from .env File

[](#loading-from-env-file)

```
use Zerotoprod\Phpdotenv\Phpdotenv;

// Read .env file and parse it
$env_file = file_get_contents(__DIR__ . '/.env');
$variables = Phpdotenv::parseFromString($env_file);

// Set variables in environment
foreach ($variables as $key => $value) {
    putenv("$key=$value");
    $_ENV[$key] = $value;
    $_SERVER[$key] = $value;
}

// Now you can access them
echo getenv('APP_NAME');  // MyApp
echo $_ENV['APP_ENV'];    // production
```

### Supported .env File Format

[](#supported-env-file-format)

The parser supports standard `.env` file syntax:

```
# Comments (lines starting with #)
# Application Settings
APP_NAME=MyApplication
APP_ENV=production
APP_DEBUG=false

# Database Configuration
DATABASE_URL="mysql://user:pass@localhost:3306/mydb"
DATABASE_POOL_SIZE=10

# Empty values
OPTIONAL_SETTING=

# Values with equals signs
CONNECTION_STRING=server=localhost;database=mydb;uid=user;pwd=pass

# Quoted values
API_KEY='secret-key-123'
API_SECRET="another-secret"

# Mixed quotes in values
DESCRIPTION="App with 'single' quotes inside"
```

### Features

[](#features)

#### Comments

[](#comments)

Lines starting with `#` are treated as comments and ignored:

```
$result = Phpdotenv::parseFromString("# This is a comment\nAPP_NAME=MyApp");
// ['APP_NAME' => 'MyApp']
```

#### Quote Handling

[](#quote-handling)

The parser automatically strips surrounding quotes from values:

```
$result = Phpdotenv::parseFromString('API_KEY="secret-123"');
// ['API_KEY' => 'secret-123']

$result = Phpdotenv::parseFromString("API_KEY='secret-123'");
// ['API_KEY' => 'secret-123']
```

#### Whitespace Trimming

[](#whitespace-trimming)

Leading and trailing whitespace is automatically trimmed from keys and values:

```
$result = Phpdotenv::parseFromString('  APP_NAME  =  MyApp  ');
// ['APP_NAME' => 'MyApp']
```

#### Empty Values

[](#empty-values)

Keys can have empty values:

```
$result = Phpdotenv::parseFromString('OPTIONAL_KEY=');
// ['OPTIONAL_KEY' => '']
```

#### Values with Equals Signs

[](#values-with-equals-signs)

Values can contain equals signs:

```
$result = Phpdotenv::parseFromString('CONNECTION=server=localhost;db=mydb');
// ['CONNECTION' => 'server=localhost;db=mydb']
```

#### Newline Format Support

[](#newline-format-support)

The `parseFromString` method handles all newline formats:

```
// Unix newlines (\n)
$result = Phpdotenv::parseFromString("KEY1=value1\nKEY2=value2");

// Windows newlines (\r\n)
$result = Phpdotenv::parseFromString("KEY1=value1\r\nKEY2=value2");

// Mac newlines (\r)
$result = Phpdotenv::parseFromString("KEY1=value1\rKEY2=value2");

// Mixed newlines
$result = Phpdotenv::parseFromString("KEY1=value1\r\nKEY2=value2\nKEY3=value3");
```

#### Empty Lines

[](#empty-lines)

Empty lines are automatically filtered out:

```
$result = Phpdotenv::parseFromString("KEY1=value1\n\n\nKEY2=value2");
// ['KEY1' => 'value1', 'KEY2' => 'value2']
```

#### Duplicate Keys

[](#duplicate-keys)

If a key appears multiple times, the last value wins:

```
$result = Phpdotenv::parseFromString("API_KEY=first\nAPI_KEY=second");
// ['API_KEY' => 'second']
```

#### Invalid Lines

[](#invalid-lines)

Lines without an equals sign are ignored:

```
$result = Phpdotenv::parseFromString("VALID=value\nINVALID_LINE\nANOTHER=value");
// ['VALID' => 'value', 'ANOTHER' => 'value']
```

### API Reference

[](#api-reference)

#### `Phpdotenv::parseFromString(string $subject): array`

[](#phpdotenvparsefromstringstring-subject-array)

Parses a string containing environment variables into an associative array.

**Parameters:**

- `$subject` (string): The string containing environment variables (with newlines)

**Returns:**

- `array`: Associative array of key-value pairs

**Example:**

```
$result = Phpdotenv::parseFromString("APP_NAME=MyApp\nAPP_ENV=production");
// ['APP_NAME' => 'MyApp', 'APP_ENV' => 'production']
```

#### `Phpdotenv::parse(array $lines): array`

[](#phpdotenvparsearray-lines-array)

Parses an array of lines into an associative array of key-value pairs.

**Parameters:**

- `$lines` (array): Array of strings, each representing a line from an env file

**Returns:**

- `array`: Associative array of key-value pairs

**Example:**

```
$result = Phpdotenv::parse(['APP_NAME=MyApp', 'APP_ENV=production']);
// ['APP_NAME' => 'MyApp', 'APP_ENV' => 'production']
```

### Pure Functions

[](#pure-functions)

Both methods are pure functions with no side effects. They:

- Do not modify the global environment (`$_ENV`, `$_SERVER`, `putenv()`)
- Do not read from the filesystem
- Simply parse and return an associative array

This design gives you full control over what happens with the parsed variables.

Contributing
------------

[](#contributing)

Contributions, issues, and feature requests are welcome! Feel free to check the [issues](https://github.com/zero-to-prod/phpdotenv/issues) page if you want to contribute.

1. Fork the repository.
2. Create a new branch (`git checkout -b feature-branch`).
3. Commit changes (`git commit -m 'Add some feature'`).
4. Push to the branch (`git push origin feature-branch`).
5. Create a new Pull Request.

###  Health Score

33

—

LowBetter than 74% of packages

Maintenance83

Actively maintained with recent releases

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity21

Early-stage or recently created project

 Bus Factor1

Top contributor holds 60% 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

Unknown

Total

1

Last Release

161d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/502649f05d36c87d494988bd99193a4d908d345335d99c080928a726277371f5?d=identicon)[zero-to-prod](/maintainers/zero-to-prod)

---

Top Contributors

[![zero-to-prod](https://avatars.githubusercontent.com/u/61474950?v=4)](https://github.com/zero-to-prod "zero-to-prod (3 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (2 commits)")

---

Tags

phpdotenvzero-to-prod

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zero-to-prod-phpdotenv/health.svg)

```
[![Health](https://phpackages.com/badges/zero-to-prod-phpdotenv/health.svg)](https://phpackages.com/packages/zero-to-prod-phpdotenv)
```

###  Alternatives

[zero-to-prod/data-model

Transforms Data into Type-Safe DTOs.

14226.2k32](/packages/zero-to-prod-data-model)[sixlive/dotenv-editor

A tool to edit phpdotenv files

761.5M11](/packages/sixlive-dotenv-editor)[beebmx/kirby-env

Enable env variables to Kirby

2037.9k2](/packages/beebmx-kirby-env)

PHPackages © 2026

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