PHPackages                             balint-horvath/dotenv-php - 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. balint-horvath/dotenv-php

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

balint-horvath/dotenv-php
=========================

Simple .env file parser and ENV loader (.env to getenv() and $\_ENV) based on standard PHP INI parser (parse\_ini\_file).

1.0.0(8y ago)0405Apache-2.0PHP &gt;=7.0

Since Oct 23Compare

[ Source](https://github.com/balint-horvath/dotenv-php)[ Packagist](https://packagist.org/packages/balint-horvath/dotenv-php)[ RSS](/packages/balint-horvath-dotenv-php/feed)WikiDiscussions Synced 2w ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

dotenv
======

[](#dotenv)

Simple .env file parser and ENV loader (`.env` to `getenv()` and `$_ENV`) based on standard PHP INI parser (parse\_ini\_file).

[![Version 1.0](https://camo.githubusercontent.com/edbb18acbc23fdd55763991efbb0632b17e3dcd9299348b97b7e583757bbb0e6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d312e302d79656c6c6f772e737667)](https://camo.githubusercontent.com/edbb18acbc23fdd55763991efbb0632b17e3dcd9299348b97b7e583757bbb0e6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e2d312e302d79656c6c6f772e737667)[![Build Passing](https://camo.githubusercontent.com/2a9926daa0ccfbd363e2e7f82ef7d6ce6e4f399075130e39dfe95a02ea642bfe/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d70617373696e672d677265656e2e737667)](https://camo.githubusercontent.com/2a9926daa0ccfbd363e2e7f82ef7d6ce6e4f399075130e39dfe95a02ea642bfe/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d70617373696e672d677265656e2e737667)[![PHP 7.0](https://camo.githubusercontent.com/1f94a7f82850b70e46350da980262316a3ecd8407475195cf8173aec09695880/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e302d626c75652e737667)](https://camo.githubusercontent.com/1f94a7f82850b70e46350da980262316a3ecd8407475195cf8173aec09695880/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d372e302d626c75652e737667)

[![balint-horvath/dotenv-php](https://camo.githubusercontent.com/70b7037d4d0b4f394fdf806dc362431a24ad354134b9d97e8a3b0cb8cc12eddb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7061636b61676973742d62616c696e74254532253830253934686f7276617468253246646f74656e762545322538302539347068702d626c75652e737667)](https://packagist.org/packages/balint-horvath/dotenv-php)[![balint-horvath/dotenv-php](https://camo.githubusercontent.com/61359459b102c894c2ab714cd122ddf7e43836286b0975a5f01289355de3d208/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6769746875622d62616c696e74254532253830253934686f7276617468253246646f74656e762545322538302539347068702d626c75652e737667)](https://github.com/balint-horvath/dotenv-php)

**Supported methods:**

- **getenv(variable)**
- **getenv(section\_variable)**
- **$\_ENV\[variable\]**
- **$\_ENV\[section\_variable\]**
- **$dotenv-&gt;variable**
- **$dotenv-&gt;section-&gt;variable**
- **$dotenv\[variable\]**
- **$dotenv\[section\]\[variable\]**

Examples
========

[](#examples)

- [Usage Example (PHP Code)](/examples/usage.php)
- [Example INI (.ini)](/examples/.env)

Installation
============

[](#installation)

To install this library, you need to use Composer in your project. If you are not using Composer yet, here's how to install:

```
curl -sS https://getcomposer.org/installer | php
```

via Composer
------------

[](#via-composer)

```
composer require balint-horvath/dotenv-php
```

### Phar

[](#phar)

```
php composer.phar require balint-horvath/dotenv-php
```

Example Environment File (INI) (.env)
=====================================

[](#example-environment-file-ini-env)

```
#.env
[API]
apiUser = User
apiKey = Key
```

Usage (Instance)
================

[](#usage-instance)

Class
-----

[](#class)

**Namespace**: `\BalintHorvath\DotEnv\`

**Class**: `DotEnv`

```
new \BalintHorvath\DotEnv\DotEnv($path)
```

Properties
----------

[](#properties)

- `(string)` **path**: Directory of .env file or full path to your ini file. (default: `../../../`)
- `(bool)` **setEnvironmentVariables**: If it's `true`, variables will be available via environment (`$_ENV`, `getenv()`), otherwise (if `false`) they'll be available only via the DotEnv as object or array (`$dotenv->` `$dotenv[]`). (default: `true`)
- `(bool)` **processSections**: If it's `true`, variables will be organized under sections (`$dotenv->section` `$dotenv[section]`), otherwise sections will have no matter. (default: `true`)
- `(bool)` **scannerMode**: If it's `INI_SCANNER_TYPED`, values `0`/`off`/`"false"`/`false` will become `bool` `false`, values `1`/`on`/`"true"`/`true` will become `bool` `true`. Can either be `INI_SCANNER_NORMAL` or `INI_SCANNER_RAW`. If `INI_SCANNER_RAW` is supplied, then option values will not be parsed. (*See [PHP Manual: parse\_ini\_file](http://php.net/manual/en/function.parse-ini-file.php) and [PHP Manual: Predefined Constants](http://php.net/manual/en/filesystem.constants.php) for more.*) (default: `INI_SCANNER_TYPED`)

Example
-------

[](#example)

```
define('APP_DIR', dirname(__FILE__) . '/');

require 'vendor/autoload.php';

$dotenv = new \BalintHorvath\DotEnv\DotEnv(APP_DIR);
```

Getting environment variables
=============================

[](#getting-environment-variables)

Object Access
-------------

[](#object-access)

**Usage:**

```
    $dotenv->{variable}
    $dotenv->{section}->{variable}
```

**Example:**

```
    $dotenv->API->apiUser
```

```
    API User:
    API Key:
```

ENV Access ($\_ENV)
-------------------

[](#env-access-_env)

**Usage:**

```
    $_ENV['{variable}']
    $_ENV['{section}']['{variable}']
```

**Example:**

```
    $_ENV['API_apiUser']
```

```
    API User:
    API Key:
```

ENV Access (getenv)
-------------------

[](#env-access-getenv)

**Usage:**

```
    getenv('variable')
    getenv('section_variable')
```

**Example:**

```
    getenv('API_apiUser')
```

```
    API User:
