A minimal and lightweight .env file parser for PHP with no dependencies.
Lite Env is a simple library for loading environment variables from .env files in PHP applications. It's designed to be lightweight and dependency-free, making it ideal for use in other libraries where minimizing dependencies is important.
Note: This is a lightweight library primarily intended for developing other libraries. For production applications, consider using more powerful and feature-rich alternatives like vlucas/phpdotenv or symfony/dotenv.
- Zero dependencies
- Simple and intuitive API
- Automatic loading of
.envand.env.localfiles - Support for variable interpolation (
${VAR}or$VARsyntax) - Type conversion (strings, integers, floats, booleans, null)
- Multiline values
- Quoted values (both single and double quotes)
- Inline comments support with proper whitespace handling
- Multiple file loading with single method call
- Comprehensive error handling and validation
composer require petstack/lite-env<?php
use PetStack\LiteEnv\Env;
// Load variables from .env and .env.local files automatically
Env::load();
// Load custom files along with default ones
Env::load('/path/to/custom.env');
// Load multiple custom files
Env::load('/path/to/.env.production', '/path/to/.env.secrets');
// Disable automatic loading of default files
Env::$disableDefaultPaths = true;
Env::load('/path/to/custom.env'); // Only loads custom.env
// Get an environment variable
$dbName = Env::get('DATABASE_NAME');
// Get with a default value if the variable doesn't exist
$port = Env::get('PORT', 3000);
// Check if a variable exists
if (Env::has('DEBUG')) {
// Do something
}
// Get all loaded environment variable keys
$keys = Env::getAllKeys();By default, Env::load() will automatically load both .env and .env.local files if they exist in the current directory. The .env.local file takes precedence over .env for overlapping variables.
// This will load .env, .env.local, and custom.env (in that order)
Env::load('custom.env');
// To load only specific files without defaults:
Env::$disableDefaultPaths = true;
Env::load('production.env', 'secrets.env');# Database configuration
DATABASE_NAME=myapp_db
DATABASE_USER=dbuser
DATABASE_PASS=secret
# Application settings
APP_ENV=development
DEBUG=true
PORT=3000
# Path configuration with variable interpolation
BASE_PATH=/var/www/app
LOG_PATH=${BASE_PATH}/logs
CACHE_PATH=$BASE_PATH/cache
# Values with inline comments (v2.0 improvement)
API_URL=https://api.example.com # Production API endpoint
TIMEOUT=30 # Connection timeout in seconds
# Multiline value
MULTILINE_VALUE="line1
line2
line3"
# Quoted values with inner quotes
QUOTED_VALUE='He said "Hello World"'
JSON_CONFIG={"key": "value", "debug": true}
Lite Env automatically converts values to appropriate PHP types:
true,(true),TRUE→true(boolean)false,(false),FALSE→false(boolean)null,(null)→nullempty,(empty),"",''→''(empty string)- Numeric values → integers or floats
If you're upgrading from v1.x, please note these breaking changes:
- Class is now final - You cannot extend the
Envclass anymore - Removed
loadMultiple()method - Useload(string ...$paths)instead:// Old (v1.x) Env::loadMultiple('/path/to/.env', '/path/to/another.env'); // New (v2.0) Env::load('/path/to/.env', '/path/to/another.env');
- Automatic default file loading -
load()now loads.envand.env.localby default
See CHANGELOG.md for detailed migration guide.
- PHP 8.0 or higher
This library is licensed under the MIT License - see the LICENSE file for details.