A lightweight .env loader for PHP projects.
Fast, Safe, Simple — designed for small to medium projects.
composer require datahihi1/tiny-env:1.0.17require 'vendor/autoload.php';
use Datahihi1\TinyEnv\TinyEnv;
$env = new TinyEnv(__DIR__);
$env->load();
echo env('DB_HOST', 'localhost');.env file:
DB_HOST=127.0.0.1
DB_PORT=3306$env->load(); // Load all
$env->load(specificKeys: ['DB_HOST']); // Load specific keys
$env->load([], forceReload: true); // Force reload (overwrite existing values)
$env->load([], noFile: true); // Load without requiring .env file to exist$env = new TinyEnv(__DIR__, true); // Load immediately and populate $_SERVER|$_ENV but only .env and not recommended for production$env->envfiles(['.env', '.env.local', '.env.production']); // Load in order, later files override earlier ones, not work with fastLoadBy default, TinyEnv rejects some values that look like dangerous PHP stream wrappers (e.g. phar:, php://, data:) to reduce the chance that an env value is later used unsafely by your app.
If you intentionally need to use a wrapper such as phar://..., you can opt-in with an allowlist:
$env = new TinyEnv(__DIR__);
$env->allowWrapperSchemes(['phar']); // opt-in to allow phar://... values
$env->load();$env = new TinyEnv(__DIR__); // By default, superglobals are NOT populated to avoid unintended side effects. You can enable it explicitly:
$env->populateSuperglobals(); // Enable superglobals population
$env->populateServerglobals(); // Enable server globals population
$env->load();Or use fastLoad which will always populate superglobals - But not recommended for production.
- Getting Values
echo env('NAME'); // Get value
echo env('NOT_FOUND', 'backup'); // With default
print_r(env()); // Get all (in .env file)
print_r(s_env()); // Get all converted to string
print_r(sysenv()); // Get all system variables- Validation
Using tiny-env-validator
- Encryption
Using tiny-env-encryptor
TinyEnv supports shell-style interpolation inside .env values:
DB_HOST=localhost
DB_PORT=3306
DB_URL=${DB_HOST}:${DB_PORT}
USER_NAME=
USER=${USER_NAME:-guest} # default if unset or empty
ALT_USER=${USER_NAME-guest} # default if unset only
REQUIRED=${MISSING?Missing variable MISSING}Result:
DB_URL = "localhost:3306"
USER = "guest" (because USER_NAME is empty)
ALT_USER = "" (because USER_NAME exists but empty)
REQUIRED → throws ExceptionNotes
- Comments start with
#.- Variable names:
A-Z,0-9,_.- Spaces around
=still valid but not recommended.- Values are auto-parsed into correct types:
"true", "yes", "on"→true"false", "no", "off"→false"123"→int"12.3"→floatordouble"null"or empty →null- TinyEnv considers yes/no, on/off to be boolean values.
- Use
"/value/"to force string type.