Bird
0
0

Given this controller method in Laravel:

medium📝 component behavior Q13 of 15
Laravel - Controllers
Given this controller method in Laravel:
public function greet($name) {
    return "Hello, " . $name . "!";
}

What will be the output if called with $controller->greet('Anna')?
A"Hello, Anna!"
B"Hello, $name!"
CError: Undefined variable
D"Hello, !"
Step-by-Step Solution
Solution:
  1. Step 1: Understand method parameter usage

    The method receives $name and concatenates it with strings.
  2. Step 2: Substitute argument value

    Calling with 'Anna' replaces $name with 'Anna', so output is "Hello, Anna!".
  3. Final Answer:

    "Hello, Anna!" -> Option A
  4. Quick Check:

    Parameter value used in string = "Hello, Anna!" [OK]
Quick Trick: Replace parameter with argument value to find output [OK]
Common Mistakes:
  • Treating $name as literal string
  • Expecting error due to missing variable
  • Ignoring string concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes