https://github.com/melbahja/environ
🐘 PHP environment variables 🔃 loader in $_ENV ONLY 🚤 with the power of INI syntax and ARRAY support
https://github.com/melbahja/environ
env environment environment-variables environment-vars ini php php-environment-loader php7 php71 php72
Last synced: 4 months ago
JSON representation
🐘 PHP environment variables 🔃 loader in $_ENV ONLY 🚤 with the power of INI syntax and ARRAY support
- Host: GitHub
- URL: https://github.com/melbahja/environ
- Owner: melbahja
- License: mit
- Created: 2018-09-23T17:26:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-10-30T11:24:06.000Z (over 4 years ago)
- Last Synced: 2025-04-09T20:04:08.725Z (10 months ago)
- Topics: env, environment, environment-variables, environment-vars, ini, php, php-environment-loader, php7, php71, php72
- Language: PHP
- Homepage:
- Size: 80.1 KB
- Stars: 10
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Environ
Load PHP environment variables from .env (INI syntax) file only on `$_ENV` and runtime.

[](https://travis-ci.org/melbahja/environ)
[](https://twitter.com/intent/tweet?text=Wow:&url=https%3A%2F%2Fgithub.com%2Fmelbahja%2Fenviron)
## Installation :
```bash
composer require melbahja/environ
```
## Why?
Some env libraries load variables into `$_SERVER` and `$_REQUEST`, which is a stupid idea that can lead to expose credentials and insert sensitive information into log files. `environ` only load variables to superglobal `$_ENV` and runtime via `putenv`.
## Usage :
`/path/to/your/project/.env`
```ini
; set a var
APP_MODE = "dev"
; array
[DATABASE]
HOST = '127.0.0.1'
USERNAME = 'root'
PASSWORD = null
```
YourScript.php
```php
require 'vendor/autoload.php';
use Melbahja\Environ\Environ;
// environ looking for .env or env.ini file in your directory
Environ::load('/path/to/your/project');
var_dump(Environ::get('APP_MODE')); // string
var_dump(Environ::get('DATABASE')); // array
var_dump($_ENV['DATABASE']); // array
```
### Note:
Arrays will not be available in `getenv()`, you can only access them via `$_ENV` or `Environ::get()`.
## Helper
```php
# if you want a helper
function env(string $var, $default = null)
{
return \Melbahja\Environ\Environ::get($var, $default);
}
```
## Environ methods :
```php
Environ::load(string $directory): bool
```
```php
Environ::get(string $var, $default = null): mixed
```
```php
Environ::set(string $var, $value): bool
```
```php
# Example: Environ::is('apache'), Environ::is('cli')
Environ::is(string $sapi): bool
```
## License :
[MIT](https://github.com/melbahja/environ/blob/master/LICENSE) Copyright (c) 2018-present Mohamed Elbahja