Bird
0
0

What will be the output of this Laravel code snippet?

medium📝 component behavior Q5 of 15
Laravel - Request and Response
What will be the output of this Laravel code snippet?
$value = $request->query('page', 1);
echo $value;

Given the URL is /items (no query parameters).
A1
Bnull
Cpage
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand query() with default

    The query() method returns the query parameter or the default if missing.
  2. Step 2: URL has no 'page' parameter

    Since 'page' is missing, the default value 1 is returned and echoed.
  3. Final Answer:

    1 -> Option A
  4. Quick Check:

    Missing query param returns default = 1 [OK]
Quick Trick: query() returns default if param missing [OK]
Common Mistakes:
  • Expecting null if param missing
  • Using input() instead of query()
  • Assuming error on missing param

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes