Zap
Documentation / Utilities

Envy

Load environment variables from a .env file and access or update them through Zap's env() and set_env() helpers. Use environment configuration for values that differ between development, testing, and production.

Load environment values

During bootstrap, Zap loads the project-root .env file when it exists. To load another file explicitly, call load_env(). Passing a directory loads the .env file inside it.

$environment = load_env(BASE_PATH . '/.env.testing');
$valuesLoaded = $environment->all();

By default, values already available in the process environment are preserved. Pass true as the second argument only when the file should replace existing values:

$environment = load_env(BASE_PATH . '/.env.testing', true);
Loading a missing, unreadable, or invalid environment file throws a RuntimeException. Handle configuration errors during setup rather than silently falling back to empty credentials.

Read a value

Use env(key, default) to read a variable and supply a fallback when it is not defined. The Envy utility checks $_ENV, then $_SERVER, then the process environment.

$appName = env('APP_NAME', 'Zap Application');
$debug = env('APP_DEBUG', false);
$mailHost = env('MAIL_HOST');

Unquoted boolean values such as true and false, and null, are parsed as PHP values. Quoted values remain strings.

Set a value at runtime

Call set_env() to update a value for the current process. The setter updates $_ENV, $_SERVER, and the process environment. Values must be scalar or null, and variable names must use letters, digits, and underscores (not beginning with a digit).

set_env('APP_DEBUG', false);

if (env('APP_DEBUG', false)) {
 // Enable development-only behavior.
}

Runtime changes do not rewrite the .env file and do not persist across separate PHP processes.

Write a .env file

Use one NAME=value assignment per line. Blank lines and lines beginning with # are ignored. Unquoted values may include a trailing comment; prefixing an assignment with export is also supported.

APP_NAME=Zap
APP_DEBUG=false
APP_URL="https://example.test"
MAIL_HOST=smtp.example.test # outbound mail
APP_LABEL=${APP_NAME} Framework
APP_LITERAL='${APP_NAME}'

Double-quoted values support escaped and multiline content. Variables referenced as ${NAME} are interpolated in unquoted and double-quoted values; single-quoted values are left uninterpolated. Circular references and malformed assignments are rejected.

Protect environment secrets

  • Keep the real .env file out of version control; commit only a safe example file.
  • Use different credentials and application keys for development, testing, and production.
  • Restrict file permissions and avoid placing environment files under the public web root.
  • Never print passwords, tokens, or database credentials into pages or logs.
  • Use config() for application configuration that is already mapped from environment values.
Next: work with arrays

Continue with the ArrayEngine utility guide.

ArrayEngine utility