Bird
0
0

What will this PHP code output?

medium📝 Predict Output Q5 of 15
PHP - Functions
What will this PHP code output?
function greet($name = 'User') {
  return "Hello, $name!";
}
echo greet('Alice');
echo greet();
AHello, Alice!Hello, Alice!
BHello, User!Hello, Alice!
CHello, Alice!Hello, User!
DHello, User!Hello, User!
Step-by-Step Solution
Solution:
  1. Step 1: Analyze first call greet('Alice')

    Argument 'Alice' overrides default, so output is "Hello, Alice!".
  2. Step 2: Analyze second call greet() without argument

    Default 'User' is used, output is "Hello, User!".
  3. Final Answer:

    Hello, Alice!Hello, User! -> Option C
  4. Quick Check:

    Default parameter used only when argument missing [OK]
Quick Trick: Explicit argument overrides default; missing uses default [OK]
Common Mistakes:
  • Assuming default always used
  • Mixing order of outputs
  • Expecting error on missing argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes