Bird
0
0

What will be the output of this Laravel code snippet?

medium📝 Predict Output Q5 of 15
Laravel - Request and Response
What will be the output of this Laravel code snippet?
public function test(Request $request) {
    $name = $request->input('name', 'Guest');
    return 'Hello, ' . $name;
}

If the request has no 'name' parameter?
AHello, null
BHello,
CError: missing parameter
DHello, Guest
Step-by-Step Solution
Solution:
  1. Step 1: Understand input() with default value

    The second argument to input() is a default returned if the key is missing.
  2. Step 2: Apply default when 'name' is missing

    Since 'name' is missing, $name becomes 'Guest'. The return is 'Hello, Guest'.
  3. Final Answer:

    Hello, Guest -> Option D
  4. Quick Check:

    input() default value used when missing [OK]
Quick Trick: Use input('key', 'default') for fallback values [OK]
Common Mistakes:
  • Expecting empty string instead of default
  • Assuming null is returned if missing
  • Thinking code throws error without parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes