Bird
0
0

Which of the following is the correct syntax to define a function with a default parameter value in PHP?

easy📝 Syntax Q12 of 15
PHP - Functions
Which of the following is the correct syntax to define a function with a default parameter value in PHP?
Afunction greet($name = "Guest") { echo "Hello, $name!"; }
Bfunction greet($name) = "Guest" { echo "Hello, $name!"; }
Cfunction greet($name: "Guest") { echo "Hello, $name!"; }
Dfunction greet($name) { $name = "Guest"; echo "Hello, $name!"; }
Step-by-Step Solution
Solution:
  1. Step 1: Review PHP default parameter syntax

    In PHP, default values are assigned using the equals sign inside the function parameter list.
  2. Step 2: Check each option

    function greet($name = "Guest") { echo "Hello, $name!"; } uses correct syntax: $name = "Guest" inside parentheses. Syntaxes using = after the closing parenthesis or colon (:) are invalid. function greet($name) { $name = "Guest"; echo "Hello, $name!"; } assigns default inside the function body, not as a parameter default.
  3. Final Answer:

    function greet($name = "Guest") { echo "Hello, $name!"; } -> Option A
  4. Quick Check:

    Default parameter = equals sign in parentheses [OK]
Quick Trick: Use = inside parentheses to set default values [OK]
Common Mistakes:
  • Using colon or other symbols instead of =
  • Assigning default inside function body instead of parameter list
  • Placing default value outside parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes