Bird
0
0

You want to create a new Laravel route that returns the current year dynamically. Which code snippet correctly does this in routes/web.php?

hard📝 Application Q15 of 15
Laravel - Basics and Architecture
You want to create a new Laravel route that returns the current year dynamically. Which code snippet correctly does this in routes/web.php?
ARoute::get('/year', function () { return date('Y'); });
BRoute::get('/year', function () { echo "date('Y')"; });
CRoute::get('/year', function () { return "Year is date('Y')"; });
DRoute::get('/year', function () { return date(d); });
Step-by-Step Solution
Solution:
  1. Step 1: Use PHP date function correctly

    The date('Y') function returns the current year as a string.
  2. Step 2: Return the year string properly in the route

    Using return date('Y'); inside the route's closure sends the year as response.
  3. Step 3: Identify errors in other options

    The echo option outputs literal "date('Y')"; another returns literal "Year is date('Y')"; the last uses date(d) without quotes, returning the day of the month instead of the year.
  4. Final Answer:

    Route::get('/year', function () { return date('Y'); }); -> Option A
  5. Quick Check:

    Return date('Y') for current year [OK]
Quick Trick: Use return with date('Y') inside route function [OK]
Common Mistakes:
  • Using echo instead of return in route
  • Forgetting quotes around 'Y' in date()
  • Returning a string that does not evaluate PHP
  • Confusing syntax for PHP date function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes