Bird
0
0

A developer tries to access a configuration value using env('APP_NAME') directly in the application code. Why might this be a bad practice?

medium📝 Debug Q7 of 15
Laravel - Configuration and Environment
A developer tries to access a configuration value using env('APP_NAME') directly in the application code. Why might this be a bad practice?
ABecause env() should only be used in config files, not in app code
BBecause env() returns null if the key is missing
CBecause env() is slower than config()
DBecause env() caches values and does not update
Step-by-Step Solution
Solution:
  1. Step 1: Understand env() usage

    env() is designed to be used in config files to load environment variables.
  2. Step 2: Why avoid env() in app code

    Using env() directly in app code can cause unexpected behavior, especially when config is cached.
  3. Final Answer:

    Because env() should only be used in config files, not in app code -> Option A
  4. Quick Check:

    Use env() only in config files = C [OK]
Quick Trick: Access env vars via config(), not env(), in app code [OK]
Common Mistakes:
  • Using env() everywhere
  • Not understanding config caching impact
  • Assuming env() updates dynamically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes