Compact and extract functions in PHP - Time & Space Complexity
We want to understand how the time it takes to run PHP's compact and extract functions changes as the number of variables grows.
How does the work inside these functions grow when we add more variables?
Analyze the time complexity of the following code snippet.
$var1 = 10;
$var2 = 20;
$var3 = 30;
// Create an array from variables
$array = compact('var1', 'var2', 'var3');
// Extract variables back from array
extract($array);
This code creates an array from named variables using compact, then restores those variables using extract.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Both
compactandextractloop through the list of variable names or array keys. - How many times: Once for each variable name given to
compactor each key in the array forextract.
As the number of variables increases, the functions do more work, roughly one step per variable.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows directly in proportion to the number of variables.
Time Complexity: O(n)
This means the time to run compact or extract grows linearly with the number of variables involved.
[X] Wrong: "compact and extract run instantly no matter how many variables there are."
[OK] Correct: Both functions must look at each variable name or array key, so more variables mean more work and more time.
Understanding how built-in functions like compact and extract scale helps you reason about performance in real projects and shows you can think about code efficiency clearly.
"What if we passed an array with nested arrays to extract? How would the time complexity change?"
Practice
compact do?Solution
Step 1: Understand the purpose of
Thecompactcompactfunction takes variable names as strings and creates an associative array where keys are those names and values are the variables' values.Step 2: Differentiate from
extractextractdoes the opposite by creating variables from array keys, so it is not the correct description forcompact.Final Answer:
Creates an array from given variable names and their values -> Option AQuick Check:
compact= array from variables [OK]
- Confusing compact with extract
- Thinking compact extracts variables
- Assuming compact converts arrays to strings
extract on an array $data?Solution
Step 1: Recall the function call syntax
In PHP, functions are called by their name followed by parentheses containing arguments. Soextract($data);is the correct syntax.Step 2: Identify incorrect syntax
Options B, C, and D misuse assignment, object operator, or array syntax, which are invalid for function calls.Final Answer:
extract($data); -> Option BQuick Check:
Function call syntax = extract($data); [OK]
- Using assignment instead of function call
- Using object operator (->) incorrectly
- Adding brackets after variable name
$name = "Alice";
$age = 30;
$arr = compact('name', 'age');
print_r($arr);
Solution
Step 1: Understand
Thecompactbehaviorcompactfunction creates an array where keys are the variable names as strings and values are the current values of those variables.Step 2: Apply to given variables
Variables$nameand$agehave values "Alice" and 30, so the array will be ['name' => 'Alice', 'age' => 30].Final Answer:
Array ( [name] => Alice [age] => 30 ) -> Option DQuick Check:
compact('name','age') = array with keys and values [OK]
- Thinking compact returns variable values as keys
- Expecting numeric keys instead of variable names
- Assuming compact prints variables directly
$data = ['color' => 'red', 'size' => 'large'];
extract($data);
echo $colour;
Solution
Step 1: Check variable names created by extract()
Theextractfunction creates variables named exactly as the array keys, so it creates$colorand$size.Step 2: Compare echoed variable name
The code tries to echo$colourwhich is different from$color, causing an undefined variable notice.Final Answer:
Variable name mismatch: $colour vs 'color' key -> Option AQuick Check:
extract keys = variable names; spelling must match [OK]
- Assuming extract changes variable names
- Thinking extract only works on indexed arrays
- Ignoring PHP notices for undefined variables
$info = ['name' => 'Bob', 'age' => 25, 'city' => 'Paris', 'age' => 30];, what will be the result of extract($info); echo $age;?Solution
Step 1: Understand PHP array key behavior
When an array has duplicate keys, the last value overwrites previous ones. Here, 'age' appears twice with values 25 and 30, so 30 is kept.Step 2: Apply extract() and echo
Extract creates variables from keys, so$agewill be 30, the last value. Echoing$ageprints 30.Final Answer:
30 -> Option CQuick Check:
Duplicate keys keep last value; extract uses that [OK]
- Expecting first duplicate key value to be used
- Thinking extract throws error on duplicates
- Assuming undefined variable error due to duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
