Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Functions
What will be the output of this PHP code?
function greet($name = "Guest") {
  return "Hello, " . $name;
}
echo greet();
AHello,
BHello, Guest
CHello, name
DError: Missing argument
Step-by-Step Solution
Solution:
  1. Step 1: Analyze function call without argument

    The function greet has a default parameter value "Guest" for $name. Calling greet() without argument uses this default.
  2. Step 2: Determine output

    The function returns "Hello, " concatenated with $name, which is "Guest" here, so output is "Hello, Guest".
  3. Final Answer:

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

    Default parameter used when argument missing = Hello, Guest [OK]
Quick Trick: Default parameters fill in missing arguments [OK]
Common Mistakes:
  • Expecting error when argument is missing
  • Ignoring default parameter values
  • Assuming empty string instead of default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes