Overview
A zero-dependency .env file parser, validator, and typed configuration container for HolyPHP.
Supports comments, export prefixes, single/double-quoted values, escape sequences, ${VAR} interpolation with defaults, and strict typed getters with clear error messages.
bundled — works offline
zero dependencies
dotenv
config
Quick start
import "dotenv";
$content = "
# Application settings
APP_NAME=\"HolyPHP Service\"
HOST=localhost
PORT=8080
BASE_URL=http://\${HOST}:\${PORT}
DEBUG=true
";
$env = DotEnv::parse($content);
$env->requireAll(["APP_NAME", "BASE_URL"]);
println($env->require("APP_NAME")); // HolyPHP Service
println($env->get("BASE_URL")); // http://localhost:8080
println($env->getInt("PORT", 3000)); // 8080
println($env->getBool("DEBUG", false)); // true
Installation
In a project
Inside a project (a folder with hphp.yaml, see Projects & packages):
hphp install envfile
This adds the library to hphp.yaml, installs it into .hphp/packages/envfile/ and pins the exact version in hphp.lock:
require:
envfile: "^1.0"
Use it
import "envfile";
envfile is bundled with the compiler, so import "envfile"; also works in a single file without installing anything, and hphp install works offline.
Outside a project
hphp install envfile # for your user, shared by all scripts
hphp create myapp # or start a project, then: hphp install envfile
Documentation
Syntax Supported
- Comments: Full-line
# ... comments and inline # ... comments (preceded by whitespace) on unquoted values.
export prefix: Optional export KEY=VALUE.
- Single quotes (
'...'): Literal strings with no escape processing or variable interpolation.
- Double quotes (
"..."): Supports \n, \r, \t, \", \\, \$, and ${VAR} interpolation.
- Interpolation:
${VAR} — expands to previously defined VAR (or empty string if unset).
${VAR:-default} — uses default if VAR is unset or empty.
${VAR-default} — uses default only if VAR is unset.
API Reference
Functions
dotenv_parse(string $content): array — Parses .env content into an associative array of key => value strings.
Class DotEnv
DotEnv::parse(string $content): DotEnv — Parses .env content and returns a DotEnv container.
new DotEnv(array $values = []) — Creates a container from an existing key-value array.
has(string $key): bool — Returns true if the key exists (even if empty).
get(string $key, $default = null) — Returns the string value or $default if the key is not set.
require(string $key): string — Returns the value or throws if missing or empty.
requireAll(array $keys): void — Validates that all listed keys exist and are non-empty; throws listing all missing keys.
getInt(string $key, $default = null): int — Parses and returns an integer, or $default if missing/empty.
getBool(string $key, $default = null): bool — Parses true/1/yes/on and false/0/no/off (case-insensitive), or returns $default if missing/empty.
all(): array — Returns the underlying associative array of all parsed key-value pairs.
Error Handling
All parse and validation errors throw Exception with a dotenv: prefix and actionable details (including line numbers for parse errors):
dotenv: missing '=' on line 3
dotenv: invalid key '123BAD' on line 1
dotenv: unterminated double-quoted value for key 'APP_NAME' on line 2
dotenv: unterminated interpolation in key 'URL' on line 4
dotenv: required key 'API_KEY' is missing or empty
dotenv: missing required keys: API_KEY, DB_URL
dotenv: key 'PORT' is not a valid integer: 'abc'
dotenv: key 'DEBUG' is not a valid boolean: 'maybe'
Examples
01Quick start
Bundled with the compiler — import "envfile"; works offline in any file, or add it to a project with hphp install envfile.
import "dotenv";
$content = "
# Application settings
APP_NAME=\"HolyPHP Service\"
HOST=localhost
PORT=8080
BASE_URL=http://\${HOST}:\${PORT}
DEBUG=true
";
$env = DotEnv::parse($content);
$env->requireAll(["APP_NAME", "BASE_URL"]);
println($env->require("APP_NAME")); // HolyPHP Service
println($env->get("BASE_URL")); // http://localhost:8080
println($env->getInt("PORT", 3000)); // 8080
println($env->getBool("DEBUG", false)); // true
Try ithphp install envfile
hphp run main.hphp
Covered by the official test suite in tests/positive/lib_envfile.hphp.