PHP Classes

PHP Environment Variables Setup: Load and initialized PHP environment variables

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-07-28 (-1 hours ago) RSS 2.0 feedNot yet rated by the usersTotal: 119 All time: 9,525 This week: 35Up
Version License PHP version Categories
environ 1.0Custom (specified...5PHP 5, Configuration, Parsers
Description 

Author

This class can load and initialized PHP environment variables.

It can take the path of a given directory and checks if a .env or .ini it exists in that directory.

The class load and parses the file that was found and sets the PHP environment variables defined in the file.

Picture of Mohamed Elbahja
  Performance   Level  
Name: Mohamed Elbahja is available for providing paid consulting. Contact Mohamed Elbahja .
Classes: 12 packages by
Country: Morocco Morocco
Age: 30
All time rank: 9001 in Morocco Morocco
Week rank: 195 Up3 in Morocco Morocco Down
Innovation award
Innovation award
Nominee: 4x

Documentation

Environ

Load PHP environment variables from .env (INI syntax) file only on $_ENV and runtime.

![](environ.jpg?raw=true) Build Status Twitter

Installation :

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


; set a var
APP_MODE = "dev"

; array
[DATABASE]
HOST = '127.0.0.1'
USERNAME = 'root'
PASSWORD = null

YourScript.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

  # if you want a helper
  function env(string $var, $default = null)
  {
    return \Melbahja\Environ\Environ::get($var, $default);
  }

Environ methods :

Environ::load(string $directory): bool
Environ::get(string $var, $default = null): mixed
Environ::set(string $var, $value): bool
# Example: Environ::is('apache'), Environ::is('cli')
Environ::is(string $sapi): bool

License :

MIT Copyright (c) 2018-present Mohamed Elbahja


  Files folder image Files (11)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Image file environ.jpg Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (11)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (11)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files (11)  /  src  
File Role Description
Files folder imageInterfaces (1 file)
  Plain text file Environ.php Class Class source

  Files folder image Files (11)  /  src  /  Interfaces  
File Role Description
  Plain text file EnvironInterface.php Class Class source

  Files folder image Files (11)  /  tests  
File Role Description
Files folder imagedirectory (1 file)
  Accessible without login Plain text file .env Data Auxiliary data
  Plain text file EnvironTest.php Class Class source

  Files folder image Files (11)  /  tests  /  directory  
File Role Description
  Accessible without login Plain text file env.ini Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:119
This week:0
All time:9,525
This week:35Up