Default parameter values in PHP - Time & Space Complexity
We want to see how using default values for function parameters affects how long a program takes to run.
Does having default values change the work the program does as input grows?
Analyze the time complexity of the following code snippet.
function greet($name = "Guest") {
echo "Hello, " . $name . "!\n";
}
for ($i = 0; $i < 5; $i++) {
greet();
greet("User" . $i);
}
This code defines a function with a default parameter and calls it multiple times with and without arguments.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop runs 5 times, calling the greet function each time.
- How many times: 10 calls total (2 calls per loop iteration times 5 iterations).
Each loop iteration does a fixed number of function calls regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 10 function calls |
| 10 | 20 function calls |
| 100 | 200 function calls |
Pattern observation: The total work grows directly with the number of loop iterations.
Time Complexity: O(n)
This means the time to run grows in a straight line as the number of loop cycles increases.
[X] Wrong: "Using default parameter values makes the function run faster or slower depending on input."
[OK] Correct: The default value is just a fallback and does not add extra work per call; the time depends on how many times the function runs, not on whether defaults are used.
Understanding how default parameters affect time helps you explain function behavior clearly and shows you know what really impacts performance.
"What if the function greet contained a loop that ran based on the length of the name string? How would the time complexity change?"
Practice
Solution
Step 1: Understand default parameters
Default parameters give a preset value to a function argument if no value is provided during the call.Step 2: Identify the purpose
This helps avoid errors and makes the function flexible by not requiring all arguments every time.Final Answer:
To provide a value if no argument is passed when calling the function -> Option CQuick Check:
Default parameter = fallback value [OK]
- Thinking default values speed up the function
- Believing default values force argument passing
- Confusing default parameters with variable declarations
Solution
Step 1: Review PHP default parameter syntax
In PHP, default values are assigned using the equals sign inside the function parameter list.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.Final Answer:
function greet($name = "Guest") { echo "Hello, $name!"; } -> Option AQuick Check:
Default parameter = equals sign in parentheses [OK]
- Using colon or other symbols instead of =
- Assigning default inside function body instead of parameter list
- Placing default value outside parentheses
function multiply($a, $b = 2) {
return $a * $b;
}
echo multiply(5);Solution
Step 1: Analyze function call with one argument
The function multiply expects two parameters, but the second has a default value of 2.Step 2: Calculate the result
Calling multiply(5) uses $a=5 and default $b=2, so 5 * 2 = 10.Final Answer:
10 -> Option DQuick Check:
5 * default 2 = 10 [OK]
- Expecting error for missing second argument
- Using wrong default value
- Confusing output with sum instead of product
function greet($name = "Guest", $age) {
echo "Hello $name, age $age";
}Solution
Step 1: Check parameter order rules
In PHP, parameters with default values must come after parameters without defaults.Step 2: Identify the error
Here, $name has a default but $age does not, and $age comes after $name, which is invalid syntax.Final Answer:
Default parameter must be last or followed only by default parameters -> Option BQuick Check:
Default parameters must be at the end [OK]
- Putting default parameters before required ones
- Ignoring PHP syntax rules for parameter order
- Thinking semicolon or variable name causes error
function createUser($name, $role = "user", $active = true) {
return "$name is a $role and active status is " . ($active ? "yes" : "no");
}
// Call the function
$result = createUser("Alice", active: false);
echo $result;What will be the output and why?
Solution
Step 1: Understand named arguments and defaults
PHP 8+ supports named arguments, so calling createUser("Alice", active: false) sets $active to false and uses default $role = "user".Step 2: Evaluate function output
$name = "Alice", $role = "user", $active = false, so output is "Alice is a user and active status is no".Final Answer:
Alice is a user and active status is no -> Option AQuick Check:
Named argument sets active=false, role defaults [OK]
- Assuming named arguments are unsupported in PHP
- Expecting syntax error due to argument order
- Confusing default parameter usage with named arguments
Ready to prove it?
Pass the test and this pattern moves unknown
