A simple library to manage environment variables in PHP

v1.0.0 2023-09-26 17:22 UTC

This package is auto-updated.

Last update: 2024-11-06 15:21:07 UTC


README

Simplifying Environment Variable Management.

Install

composer require natanael-oliveira/dot-env

Example of use

A new .env file will be created at the root of your project, you can manually insert your environment variables or define them dynamically using the set() method:

<?php

require 'vendor/autoload.php';
use NatanaelOliveira\DotEnv\Environment;

// Create an instance of the Environment class
$env = new Environment();

// Set the environment variables
$env->set('DB_HOST', 'host');
$env->set('DB_USER', 'user');
$env->set('DB_PASS', 'password');
$env->set('DB_PREFIX', 'db_prefix_');

// Get environment variables
$dbHost = $env->get('DB_HOST');
$dbUser = $env->get('DB_USER', 'default_user');
$dbPass = $env->get('DB_PASS');

// Remove environment variables
$env->remove('DB_PREFIX');

// Use environment variables in your application
echo "DB Host: $dbHost<br>";
echo "DB User: $dbUser<br>";
echo "DB Password: $dbPass<br>";

Requirements

This library needs PHP 7.4 or greater.