Bird
0
0

Which of the following is the correct nullable type syntax to declare a function parameter that accepts a nullable string in PHP?

easy📝 Syntax Q12 of 15
PHP - Functions
Which of the following is the correct nullable type syntax to declare a function parameter that accepts a nullable string in PHP?
Afunction greet(string? $name) {}
Bfunction greet(string|null $name) {}
Cfunction greet(?string $name) {}
Dfunction greet($name?: string) {}
Step-by-Step Solution
Solution:
  1. Step 1: Recall nullable type syntax in PHP

    Nullable types use a question mark ? before the type, like ?string.
  2. Step 2: Check each option's syntax

    function greet(?string $name) {} uses ?string correctly. function greet(string|null $name) {} uses union type syntax which is valid in PHP 8+, but the question asks for nullable syntax specifically. Options C and D are invalid syntax.
  3. Final Answer:

    function greet(?string $name) {} -> Option C
  4. Quick Check:

    Nullable syntax = ?type before parameter [OK]
Quick Trick: Use ? before type for nullable, not after or inside parentheses [OK]
Common Mistakes:
  • Placing ? after the type name
  • Using union type syntax instead of nullable syntax
  • Misplacing the question mark in parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes