Always validate environment variables before use and implement proper fallback strategies. For required variables, fail fast with clear error messages. For optional variables with empty values, ensure they don't short-circuit fallback chains.
Always validate environment variables before use and implement proper fallback strategies. For required variables, fail fast with clear error messages. For optional variables with empty values, ensure they don’t short-circuit fallback chains.
// Bad: Empty value leads to malformed URL
$hostname = System::getEnv('_APP_CONSOLE_DOMAIN', System::getEnv('_APP_DOMAIN', ''));
// Good: Check for truthy value before fallback
$hostname = System::getEnv('_APP_CONSOLE_DOMAIN', null);
if (empty($hostname)) {
$hostname = System::getEnv('_APP_DOMAIN', '');
if (empty($hostname)) {
throw new \RuntimeException('Required environment variable _APP_DOMAIN is not set');
}
}
When implementing environment variable handling:
intval()
for numeric settings, checking IP formats)Enter the URL of a public GitHub repository