https://github.com/phoole/env
Simple environment variables loader library for PHP
https://github.com/phoole/env
env environment phoole php swoole
Last synced: 5 months ago
JSON representation
Simple environment variables loader library for PHP
- Host: GitHub
- URL: https://github.com/phoole/env
- Owner: phoole
- License: apache-2.0
- Created: 2019-09-26T05:18:30.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-19T07:01:40.000Z (over 6 years ago)
- Last Synced: 2025-07-20T09:52:25.912Z (11 months ago)
- Topics: env, environment, phoole, php, swoole
- Language: PHP
- Homepage:
- Size: 29.3 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# env
[](https://travis-ci.com/phoole/env)
[](https://scrutinizer-ci.com/g/phoole/env/?branch=master)
[](https://codeclimate.com/github/phoole/env)
[](https://packagist.org/packages/phoole/env)
[](https://packagist.org/packages/phoole/env)
[]()
Simple environment variables loader library for PHP. It loads environment variables
from a .env file. It requires PHP 7.2+ and is compliant with [PSR-1][PSR-1],
[PSR-4][PSR-4], [PSR-12][PSR-12].
[PSR-1]: http://www.php-fig.org/psr/psr-1/ "PSR-1: Basic Coding Standard"
[PSR-4]: http://www.php-fig.org/psr/psr-4/ "PSR-4: Autoloader"
[PSR-12]: http://www.php-fig.org/psr/psr-2/ "PSR-12: Extended Coding Style Guide"
[variable]: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html "Shell Variable Expansion"
Installation
---
Install via the `composer` utility.
```
composer require "phoole/env"
```
or add the following lines to your `composer.json`
```json
{
"require": {
"phoole/env": "1.*"
}
}
```
Usage
---
- Put your environments in file `.env`. By default, existing environment variables are **not overwritten**.
```shell
# this is comment line
BASE_DIR = /usr/local # spaces allowed
# use reference here
APP_DIR = ${BASE_DIR}/app # /usr/local/app
# use bash style :- or :=
TMP_DIR = ${SYSTEM_TMP_DIR:-${APP_DIR}/tmp}
```
See [shell variable expansion][variable] for `:-` and `:=` usage.
- Load and use your env variables in PHP script
```php
load(__DIR__ . '/.env');
// use env values
echo getenv('APP_DIR');
```
Features
---
- Support shell default values, `${param:-new}` or `${param:=new}`
- By default, **WILL NOT** overwrite any existing environment variables. which
makes possible for importing environment variables from command line in the
case of docker.
To overwrite existing env variables,
```php
env->load('./.env', TRUE);
```
- Relaxed syntax (not compatible with bash) in env file
```php
# spaces before and after '=' is allowed. NOT recommended though
ROOT_DIR = /var/tmp
# same as above
ROOT_DIR=/var/tmp
# same as above
ROOT_DIR="/var/tmp"
```
- [PSR-1][PSR-1], [PSR-4][PSR-4], [PSR-12][PSR-12] compliant.
Testing
---
```bash
$ composer test
```
Dependencies
---
- PHP >= 7.2.0
License
---
- [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0)