Bird
0
0

What will be the output of this Laravel code?

medium📝 state output Q13 of 15
Laravel - Request and Response
What will be the output of this Laravel code?
session()->put('name', 'Alice');
$name = session('name');
echo $name;
Anull
BAlice
Csession
DError
Step-by-Step Solution
Solution:
  1. Step 1: Store 'Alice' in session under 'name'

    The code uses session()->put('name', 'Alice') to save the value 'Alice'.
  2. Step 2: Retrieve and echo the stored value

    session('name') fetches the value 'Alice' from session and echoes it.
  3. Final Answer:

    Alice -> Option B
  4. Quick Check:

    Stored value 'Alice' is retrieved and printed [OK]
Quick Trick: session('key') gets stored value; here it prints 'Alice' [OK]
Common Mistakes:
  • Expecting null if session not started
  • Confusing session() helper with facade
  • Assuming session() returns array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes