Nullable types in functions in PHP - Time & Space Complexity
We want to see how using nullable types in functions affects the time it takes to run the code.
Does allowing a value to be null change how long the function takes to finish?
Analyze the time complexity of the following code snippet.
function processValue(?int $value): int {
if ($value === null) {
return 0;
}
return $value * 2;
}
$result = processValue(10);
$resultNull = processValue(null);
This function takes an integer or null, checks if it is null, and returns a result accordingly.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated steps; just a simple check and calculation.
- How many times: The function runs once per call, doing a fixed number of steps.
The function does the same small number of steps no matter what number or null you give it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 3 steps |
| 100 | 3 steps |
| 1000 | 3 steps |
Pattern observation: The number of steps stays the same no matter the input size.
Time Complexity: O(1)
This means the function takes the same short time to run no matter what input it gets.
[X] Wrong: "Nullable types make the function slower because it has to check for null every time."
[OK] Correct: The null check is just one simple step and does not grow with input size, so it does not slow down the function noticeably.
Understanding how simple checks like nullable types affect performance helps you explain your code clearly and shows you think about efficiency, which is a great skill to have.
"What if the function processed an array of nullable integers instead of a single nullable integer? How would the time complexity change?"
Practice
?int mean in a PHP function parameter?Solution
Step 1: Understand nullable type syntax
The?beforeintmeans the parameter can acceptnullor anintvalue.Step 2: Interpret the meaning in function parameters
This allows the function to safely accept missing or empty values represented bynull.Final Answer:
The parameter can be either an integer or null. -> Option BQuick Check:
Nullable type = int or null [OK]
- Thinking nullable means optional parameter
- Assuming nullable excludes null values
- Confusing nullable with default values
Solution
Step 1: Recall nullable type syntax in PHP
Nullable types use a question mark?before the type, like?string.Step 2: Check each option's syntax
function greet(?string $name) {} uses?stringcorrectly. 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.Final Answer:
function greet(?string $name) {} -> Option CQuick Check:
Nullable syntax = ?type before parameter [OK]
- Placing ? after the type name
- Using union type syntax instead of nullable syntax
- Misplacing the question mark in parameter
function checkAge(?int $age) {
if ($age === null) {
return "Age not provided";
}
return "Age is $age";
}
echo checkAge(null);Solution
Step 1: Understand nullable parameter and null check
The function accepts an?int, sonullis allowed. It checks if$age === nulland returns "Age not provided".Step 2: Call the function with null
CallingcheckAge(null)triggers the null check, so it returns "Age not provided".Final Answer:
Age not provided -> Option AQuick Check:
Nullable param null input returns null check message [OK]
- Assuming null converts to empty string
- Expecting type error on null input
- Ignoring strict null check with ===
function setName(?string name) {
echo $name;
}Solution
Step 1: Check parameter syntax
In PHP, all variable names must start with a dollar sign$. The parameternameis missing$.Step 2: Validate nullable type usage
Nullable types like?stringare valid. The function does not require a return value. Parameters can be nullable without default values.Final Answer:
Missing dollar sign ($) before parameter name. -> Option AQuick Check:
Parameter variables need $ prefix [OK]
- Omitting $ in parameter names
- Thinking nullable types need default values
- Expecting functions must return values
Solution
Step 1: Check nullable parameter and return type
The function accepts?floatand returnsfloat. It must handlenullsafely and return a float.Step 2: Analyze each option's logic
function square(?float $num): float { return $num * $num; } does not check for null and will cause an error if$numis null. function square(?float $num): float { if ($num === null) { return 0; } return $num ** 2; } checks for null explicitly and returns 0, then squares the number correctly. function square(float $num = null): float { return $num * $num; } uses default null but parameter is not nullable, causing a type error. function square(?float $num): ?float { return $num ? $num * $num : 0; } returns nullable float but uses a loose check$num ?which fails for 0.0 (falsey), so it returns 0 incorrectly.Final Answer:
function square(?float $num): float { if ($num === null) { return 0; } return $num ** 2; } -> Option DQuick Check:
Check null explicitly, return float, use strict null check [OK]
- Not checking for null before calculation
- Using loose truthy checks that fail on 0.0
- Mismatching return types with nullable parameters
Ready to prove it?
Pass the test and this pattern moves unknown
