A simple and lightweight .env file loader utility for PHP. This package provides a straightforward way to load environment variables from .env
files without the overhead of larger packages.
- ✅ Simple and lightweight
- ✅ No external dependencies
- ✅ Supports quoted values (single and double quotes)
- ✅ Skips comments and empty lines
- ✅ Does not overwrite existing environment variables
- ✅ Environment-specific files (.env.dev, .env.prod, etc.)
- ✅ Local override files (.env.local, .env.dev.local, etc.)
- ✅ PHP 8.1+ support
- ✅ Full test coverage
- ✅ Static analysis compliant
- ✅ Automated dependency updates with Dependabot
Install via Composer:
composer require lukaszzychal/env-loader
<?php
use LukaszZychal\EnvLoader\EnvLoader;
// Load environment variables from .env file
$success = EnvLoader::load('.env');
if ($success) {
// Get environment variable with default value
$dbHost = EnvLoader::get('DB_HOST', 'localhost');
$dbPort = EnvLoader::get('DB_PORT', 3306);
// Check if variable exists
if (EnvLoader::has('API_KEY')) {
$apiKey = EnvLoader::get('API_KEY');
}
}
<?php
use LukaszZychal\EnvLoader\EnvLoader;
// Load with environment-specific files
EnvLoader::load('.env', 'dev'); // Loads .env and .env.dev
// File loading order (later files override earlier ones):
// 1. .env
// 2. .env.dev
// 3. .env.local
// 4. .env.dev.local
<?php
use LukaszZychal\EnvLoader\EnvLoader;
// Load with local overrides (default behavior)
EnvLoader::load('.env', 'dev', true);
// Disable local overrides
EnvLoader::load('.env', 'dev', false);
<?php
use LukaszZychal\EnvLoader\EnvLoader;
// Load and return variables without setting them globally
$variables = EnvLoader::loadAndReturn('.env');
foreach ($variables as $key => $value) {
echo "$key = $value\n";
}
The package supports standard .env
file format with environment-specific and local override files:
project/
├── .env # Base configuration
├── .env.dev # Development environment
├── .env.prod # Production environment
├── .env.staging # Staging environment
├── .env.local # Local overrides (not in git)
├── .env.dev.local # Local dev overrides (not in git)
└── .env.prod.local # Local prod overrides (not in git)
# Database configuration
DB_HOST=localhost
DB_PORT=3306
DB_NAME=myapp
DB_USER=root
DB_PASSWORD=secret
# API configuration
API_KEY="your-api-key-here"
API_URL='https://api.example.com'
# Feature flags
DEBUG=false
CACHE_ENABLED=true
# Comments are supported
# This is a comment
FEATURE_NEW_UI=false
.env.dev (Development):
# Override for development
DEBUG=true
CACHE_ENABLED=false
DB_NAME=myapp_dev
API_URL='https://dev-api.example.com'
.env.prod (Production):
# Production overrides
DEBUG=false
CACHE_ENABLED=true
DB_HOST=prod-db.example.com
API_URL='https://api.example.com'
.env.local (Local developer overrides):
# Personal local settings (not committed to git)
DB_PASSWORD=my_local_password
API_KEY=my_local_api_key
DEBUG=true
.env.dev.local (Local dev overrides):
# Personal dev settings
DB_PASSWORD=dev_password
FEATURE_NEW_UI=true
- Comments: Lines starting with
#
are ignored - Empty lines: Blank lines are skipped
- Quoted values: Both single and double quotes are supported and automatically removed
- No overwrite: Existing environment variables are not overwritten
EnvLoader::load(string $filePath, ?string $environment = null, bool $loadLocalOverrides = true): bool
Loads environment variables from .env
files with environment-specific and local override support.
Parameters:
$filePath
(string): Path to the base .env file$environment
(string|null): Environment name (e.g., 'dev', 'prod', 'staging')$loadLocalOverrides
(bool): Whether to load .local override files
Returns:
bool
:true
if any file was loaded successfully,false
otherwise
File Loading Order:
- Base file (e.g.,
.env
) - Environment-specific file (e.g.,
.env.dev
) - Local override file (e.g.,
.env.local
) - Environment-specific local override (e.g.,
.env.dev.local
)
Gets an environment variable value.
Parameters:
$key
(string): Environment variable key$default
(mixed): Default value if key is not found
Returns:
mixed
: Environment variable value or default
Checks if an environment variable exists.
Parameters:
$key
(string): Environment variable key
Returns:
bool
:true
if variable exists,false
otherwise
EnvLoader::loadAndReturn(string $filePath, ?string $environment = null, bool $loadLocalOverrides = true): array
Loads environment variables from .env
files and returns them as an array with environment-specific and local override support.
Parameters:
$filePath
(string): Path to the base .env file$environment
(string|null): Environment name (e.g., 'dev', 'prod', 'staging')$loadLocalOverrides
(bool): Whether to load .local override files
Returns:
array
: Array of loaded environment variables
Run the test suite:
composer test
Run tests with coverage:
composer test-coverage
Run static analysis:
composer phpstan
Check code style:
composer cs-check
Fix code style issues:
composer cs-fix
Run all quality checks:
composer quality
- PHP 8.1 or higher
This project is licensed under the MIT License. See the LICENSE file for details.
This project uses Dependabot to automatically:
- 🔄 Monitor dependencies for security vulnerabilities
- 📦 Update Composer packages weekly
- ⚙️ Update GitHub Actions weekly
- 🛡️ Create security PRs for critical vulnerabilities
- 📋 Group minor/patch updates to reduce PR noise
Dependabot will create pull requests for dependency updates. Review and merge them to keep your dependencies secure and up-to-date.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project uses automated releases via GitHub Actions. When you push a Git tag, the following happens automatically:
- Tests and quality checks run
- GitHub Release is created with changelog
- Packagist is notified (if webhook configured)
Use the included release script:
./release.sh 1.0.0
Or manually:
git tag v1.0.0
git push origin v1.0.0
- Initial release
- Basic .env file loading functionality
- Support for quoted values
- Comment and empty line handling
- Non-overwriting behavior for existing environment variables
- Automated release workflow with GitHub Actions
Lukasz Zychal
- GitHub: @lukaszzychal
This package is inspired by the need for a lightweight alternative to larger .env loading libraries while maintaining simplicity and performance.