Function declaration and calling in PHP - Time & Space Complexity
When we declare and call a function, we want to know how long it takes as the input changes.
We ask: How does the time grow when we run the function with bigger inputs?
Analyze the time complexity of the following code snippet.
function greet($name) {
echo "Hello, $name!\n";
}
$names = ['Alice', 'Bob', 'Charlie'];
foreach ($names as $person) {
greet($person);
}
This code defines a function that prints a greeting and calls it for each name in a list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the greet function inside a loop.
- How many times: Once for each name in the array.
Each time we add a new name, the function runs one more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 greetings printed |
| 100 | 100 greetings printed |
| 1000 | 1000 greetings printed |
Pattern observation: The work grows directly with the number of names.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of inputs.
[X] Wrong: "Calling a function once always takes the same time no matter what."
[OK] Correct: The function might be called many times, so total time depends on how many calls happen.
Understanding how function calls add up helps you explain code efficiency clearly and confidently.
"What if the greet function itself had a loop inside? How would the time complexity change?"
Practice
greet in PHP?Solution
Step 1: Recognize PHP function declaration syntax
In PHP, functions are declared using the keywordfunctionfollowed by the function name and parentheses.Step 2: Match the correct syntax
function greet() { } usesfunction greet() { }, which is the correct PHP syntax for declaring a function namedgreet.Final Answer:
function greet() { } -> Option AQuick Check:
PHP function declaration = function greet() { } [OK]
- Using 'def' instead of 'function'
- Adding a colon after 'function'
- Using 'func' keyword which is not valid in PHP
sayHello in PHP?Solution
Step 1: Understand PHP function call syntax
In PHP, to call a function, you write the function name followed by parentheses, optionally with arguments inside.Step 2: Identify the correct option
sayHello(); usessayHello();, which is the correct way to call the function namedsayHello.Final Answer:
sayHello(); -> Option BQuick Check:
Function call syntax = sayHello(); [OK]
- Using 'call' keyword which does not exist in PHP
- Omitting parentheses when calling functions
- Using incorrect function call syntax like callFunction()
function add($a, $b) {
return $a + $b;
}
echo add(3, 4);Solution
Step 1: Understand the function behavior
The functionaddtakes two parameters and returns their sum.Step 2: Calculate the return value
Callingadd(3, 4)returns 3 + 4 = 7, which is then printed byecho.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Concatenating numbers as strings instead of adding
- Expecting function name to print instead of return value
- Syntax errors from missing semicolons
function greet() {
echo "Hello!";
}
greet;Solution
Step 1: Check function call syntax
The functiongreetis declared correctly, but when calling it, parentheses are missing.Step 2: Understand PHP function call requirements
In PHP, functions must be called with parentheses, even if they take no arguments. Sogreet;is invalid; it should begreet();.Final Answer:
Missing parentheses when calling the function -> Option CQuick Check:
Function calls need () even if no arguments [OK]
- Calling functions without parentheses
- Confusing function declaration with call
- Missing semicolons elsewhere
function multiply($x, $y = 2) {
return $x * $y;
}
echo multiply(5);What will be the output and why?
Solution
Step 1: Understand default parameter usage
The functionmultiplyhas two parameters, where$yhas a default value of 2.Step 2: Analyze the function call
Callingmultiply(5)passes 5 as$x, and since$yis not provided, it uses the default 2.Step 3: Calculate the result
The function returns 5 * 2 = 10, which is printed byecho.Final Answer:
10 because the second parameter uses default value 2 -> Option DQuick Check:
5 * 2 = 10 with default parameter [OK]
- Assuming all parameters must be passed
- Ignoring default values
- Expecting error when missing arguments
Ready to prove it?
Pass the test and this pattern moves unknown
