Nullable types in functions
📖 Scenario: You are building a simple PHP program that handles user profiles. Sometimes, the user may not provide their middle name, so your function should accept a nullable string for the middle name.
🎯 Goal: Create a PHP function that accepts a nullable string parameter for the middle name and returns a greeting message that includes the middle name if provided, or a default message if not.
📋 What You'll Learn
Create a function called
greetUser that accepts a nullable string parameter called $middleNameInside the function, check if
$middleName is nullReturn a greeting message that includes the middle name if it is not null
Return a different greeting message if
$middleName is nullCall the function twice: once with a middle name and once with null
Print the results of both function calls
💡 Why This Matters
🌍 Real World
Nullable types are common when dealing with optional user input, like middle names or phone numbers, where the value might be missing.
💼 Career
Understanding nullable types helps you write safer PHP functions that handle missing or optional data without errors.
Progress0 / 4 steps