0
0
PHPprogramming~10 mins

Nullable types in functions in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Nullable types in functions
Function Declaration
Parameter Type: Nullable
Function Call with Value
Function Executes Normally
Function Call with null
Function Accepts null Without Error
Return or Output
The function declares a parameter that can be a type or null. When called, it accepts either a value of that type or null, allowing flexible inputs.
Execution Sample
PHP
<?php
declare(strict_types=1);
function greet(?string $name) {
  if ($name === null) {
    return "Hello, Guest!";
  }
  return "Hello, $name!";
}

echo greet("Alice");
echo greet(null);
This function greets a person by name or says 'Hello, Guest!' if null is passed.
Execution Table
StepFunction CallParameter $nameCondition $name === nullReturn ValueOutput
1greet("Alice")"Alice"False"Hello, Alice!""Hello, Alice!"
2greet(null)nullTrue"Hello, Guest!""Hello, Guest!"
3End---No more calls
💡 No more function calls; execution ends.
Variable Tracker
VariableStartAfter Call 1After Call 2Final
$name-"Alice"nullnull or string
Key Moments - 2 Insights
Why can the function accept null even though the parameter type is string?
Because the parameter is declared as ?string, which means it can be a string or null, as shown in execution_table row 2 where $name is null and accepted.
What happens if we pass a non-string and non-null value?
PHP will throw a TypeError because the parameter expects either a string or null, not other types. This is not shown in the table but is important to remember.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the return value when greet(null) is called?
A"Hello, Guest!"
B"Hello, null!"
Cnull
DError
💡 Hint
Check execution_table row 2 under 'Return Value'
At which step does the condition $name === null evaluate to True?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Look at the 'Condition $name === null' column in execution_table
If we remove the question mark from ?string in the function parameter, what would happen when calling greet(null)?
AFunction returns "Hello, Guest!"
BPHP throws a TypeError
CFunction returns "Hello, null!"
DFunction treats null as empty string
💡 Hint
Nullable types allow null; removing ? disallows null, causing error on null input
Concept Snapshot
Nullable types in PHP functions:
- Use ? before type to allow null (e.g., ?string)
- Function accepts value of type or null
- Check for null inside function to handle special cases
- Passing null without ? causes TypeError
- Enables flexible and safe function inputs
Full Transcript
This example shows a PHP function greet with a nullable string parameter. The question mark before string means the parameter can be a string or null. When calling greet with "Alice", the function returns "Hello, Alice!". When calling greet with null, the function returns "Hello, Guest!" because it checks if the parameter is null. This allows the function to handle missing or optional names safely. If the question mark is removed, passing null would cause an error. This trace helps understand how nullable types work in PHP functions.