🔴 Laravel
Beginner
What is the env() helper and how should it be used?
Answer
The env() helper reads values from the .env file. Example: env("DB_HOST", "127.0.0.1") — the second argument is the default if the key is not set. However, you should never call env() directly in application code. Once php artisan config:cache is run (which is done in production for performance), all config values are cached as a PHP file and the .env file is no longer read — env() would return null. Instead, use env() only inside config/ files, and access those values throughout your application via the config() helper. This ensures config caching works correctly. Always add any new .env variables to .env.example as a template for other developers.