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

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

prinx/dotenv
============

Make easy access to the environment variables in your application

v1.0.0(5y ago)13.0k11MITPHPPHP &gt;=5.6.0CI failing

Since Mar 27Pushed 5y ago1 watchersCompare

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

READMEChangelog (9)Dependencies (2)Versions (10)Used By (11)

PHP Dotenv
==========

[](#php-dotenv)

[![tests](https://github.com/prinx/dotenv/actions/workflows/tests.yml/badge.svg)](https://github.com/prinx/dotenv/actions/workflows/tests.yml)[![](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://travis-ci.com/prinx/dotenv)[![codecov](https://camo.githubusercontent.com/faf722a27cd4020a428622d1232bcb2ce556e359d6a2dc75f15befc4d743e8be/68747470733a2f2f636f6465636f762e696f2f67682f7072696e782f646f74656e762f6272616e63682f6d61696e2f67726170682f62616467652e7376673f746f6b656e3d5a4c4b36545158454451)](https://codecov.io/gh/prinx/dotenv)

Get easily access to environment variables.

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

[](#installation)

Open a command prompt into your project root folder and run:

```
composer require prinx/dotenv
```

Usage
-----

[](#usage)

### Quick start

[](#quick-start)

```
// Require composer autoload file if it has not been done yet.
require_once __DIR__ . '/path/to/vendor/autoload.php';

/*
 * Retrieve an environment variable. Returns null if variable not found.
 */
$hostname = env('DEV_DB_HOST');

/*
 * Retrieve an environment variable. Returns default value passed as second argument if variable not found
 */
$port = env('DEV_DB_PORT', 3306);

/*
 * Add a variable to the current loaded environment (will not save in the .env file)
 */
addenv('LOG_LEVEL', 'info');

/*
 * Wrtie variable to the env file.
 * Will also automatically load the variable into the current environment.
 * If the file already contains the variable, the variable will be overwritten.
 */
persistenv('LOG_LEVEL', 'warn');
persistenv('LOG_LEVEL', 'debug');
persistenv('LOG_LEVEL', 'info');

/*
 * Get all environment variables
 */
env()
// OR
allenv();
```

### Writing a .env file

[](#writing-a-env-file)

The .env file format will be:

```
VARIABLE_NAME=value
```

For example:

```
SESSION_DRIVER=file
DEV_DB_HOST=localhost
DEV_DB_PORT=3306

PROD_DB_HOST=prod_db_ip
PROD_DB_PORT=3308
```

> As standard, the variable name is capital letter with underscores to separate words.

#### Comments

[](#comments)

You can write comments in your .env file by preceding the comment by a hash (`#`). Example:

```
# Supported: file|database
SESSION_DRIVER=file
```

#### Types of values

[](#types-of-values)

By default, env variable will be retrieved as string, except booleans, and null.

##### String

[](#string)

You can use quotes to surround strings.

```
APP_NAME=My app
# or
APP_NAME="My app"

DB_HOST=173.0.0.0
# or
DB_HOST="173.0.0.0"
```

##### Boolean

[](#boolean)

The values `true`, `"true"` or `'true'`, will be got as the boolean `true`. The values `false`, `"false"` or `'false'` will be got as the boolean `false`.

```
# Will be got as a boolean true
APP_DEBUG=true

# Will be got as a boolean false
APP_DEBUG=false
```

Same as:

```
APP_DEBUG="true"

APP_DEBUG="false"
```

##### Null

[](#null)

The values `null`, `"null"` or `'null'`, will be got as `null`.

```
# Will be got as a null
APP_DEBUG=null

APP_DEBUG="null"
```

### Referring to another variable

[](#referring-to-another-variable)

You can refer to the value of another variable in your .env file by putting the name of the variable you are referring to variable inside ${}:

```
# .env
SESSION_DRIVER=mysql
MESSAGE=App based on ${SESSION_DRIVER} database
```

```
// PHP
echo env('MESSAGE'); // App based on mysql database
```

Loading a specific .env file
----------------------------

[](#loading-a-specific-env-file)

By default, the package automatically look for the .env file in the project root folder. But you can load the env file from anywhere by using the `loadenv` function:

```
// Require composer autoload file if it has not been done yet.
require_once __DIR__ . '/path/to/vendor/autoload.php';

loadenv('/path/to/somewhere/.env');

// Then everything goes as usual
$apiKey = env('API_KEY');
```

Using the Dotenv instance
-------------------------

[](#using-the-dotenv-instance)

You can also get or set a variable using the Dotenv class instance:

The Dotenv instance can be accessed by calling the `dotenv()` function:

```
$dotenv = dotenv();
```

### Getting a variable

[](#getting-a-variable)

```
$hostname = dotenv()->get('DEV_DB_HOST');

// With a default value
$hostname = dotenv()->get('DEV_DB_HOST', 'localhost');
```

### Getting all variables

[](#getting-all-variables)

```
$hostname = dotenv()->all();

// or use get without any parameter
$hostname = dotenv()->get();
```

### Adding a variable to the current loaded environment

[](#adding-a-variable-to-the-current-loaded-environment)

```
dotenv()->add('GUEST_NAME', 'john');
```

### Writing a variable to the .env file

[](#writing-a-variable-to-the-env-file)

```
dotenv()->persist('GUEST_NAME', 'john');
```

Create your own Dotenv instance
-------------------------------

[](#create-your-own-dotenv-instance)

You can create your own Dotenv instance just by using the `Dotenv` class:

```
use Prinx\Dotenv;

$dotenv = new Dotenv('path/to/.env');

$dotenv->get('VARIABLE');
```

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

[](#contributing)

- Give a star to the repo ☺️
- Fork the repo.
- Correct a bug, add a new feature.
- Write tests.
- Create a pull request.

License
-------

[](#license)

[MIT](LICENSE)

###  Health Score

31

—

LowBetter than 65% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity58

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

Recently: every ~74 days

Total

9

Last Release

1904d ago

Major Versions

v0.5.0 → v1.0.02021-05-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/896d24b4882562faf6f1c8c25ea90e515037f5811f251a11fd80b68db61fc458?d=identicon)[Prinx](/maintainers/Prinx)

---

Top Contributors

[![prinx](https://avatars.githubusercontent.com/u/7337610?v=4)](https://github.com/prinx "prinx (128 commits)")

---

Tags

envdotenvphpdotenv

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[vlucas/phpdotenv

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

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

Registers environment variables from a .env file

3.8k249.2M3.0k](/packages/symfony-dotenv)[helhum/dotenv-connector

Makes it possible to set environment variables for composer projects.

1595.0M42](/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.

6413.0M25](/packages/m1-env)[sroze/companienv

Companion for .env files

246426.3k1](/packages/sroze-companienv)[sixlive/dotenv-editor

A tool to edit phpdotenv files

741.6M12](/packages/sixlive-dotenv-editor)

PHPackages © 2026

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