0
0
PHPprogramming~10 mins

Compact and extract functions in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Compact and extract functions
Define variables
Call compact()
Create array with variable names as keys
Call extract()
Create variables from array keys
Use variables
End
Variables are packed into an array with compact(), then unpacked back into variables with extract().
Execution Sample
PHP
<?php
$a = 10;
$b = 20;
$arr = compact('a', 'b');
extract($arr);
echo $a + $b;
?>
This code packs variables a and b into an array, then extracts them back, and prints their sum.
Execution Table
StepActionVariables BeforeResultVariables AfterOutput
1Define $a=10, $b=20noneVariables created$a=10, $b=20
2Call compact('a','b')$a=10, $b=20Array ['a'=>10, 'b'=>20]$a=10, $b=20, $arr=['a'=>10,'b'=>20]
3Call extract($arr)$a=10, $b=20, $arr=['a'=>10,'b'=>20]Variables $a and $b set from array$a=10, $b=20, $arr=['a'=>10,'b'=>20]
4Calculate echo $a + $b$a=10, $b=20, $arr=['a'=>10,'b'=>20]Sum is 30$a=10, $b=20, $arr=['a'=>10,'b'=>20]30
5End of script$a=10, $b=20, $arr=['a'=>10,'b'=>20]Script ends$a=10, $b=20, $arr=['a'=>10,'b'=>20]
💡 Script ends after printing the sum 30.
Variable Tracker
VariableStartAfter compactAfter extractFinal
$aundefined101010
$bundefined202020
$arrundefined['a'=>10,'b'=>20]['a'=>10,'b'=>20]['a'=>10,'b'=>20]
Key Moments - 3 Insights
Why does extract() create variables even if they already exist?
extract() overwrites existing variables with values from the array, as shown in step 3 of the execution_table.
What happens if compact() is called with a variable name that does not exist?
compact() ignores non-existing variables and does not include them in the array, so they won't be extracted later.
Does extract() create variables in the current scope or globally?
extract() creates variables in the current scope, so they can be used immediately after extraction, as seen in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does compact('a','b') return?
AAn array with keys 'a' and 'b' and their values
BA string concatenation of $a and $b
CAn error because variables are not passed by reference
DAn empty array
💡 Hint
Check the Result column at step 2 in execution_table.
At which step does extract() set variables from the array?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the extract() call in the Action column in execution_table.
If $b was not defined before compact(), what would $arr contain?
AEmpty array
BArray with 'a' and 'b' keys, 'b' value null
CArray with only 'a' key and its value
DArray with 'b' key only
💡 Hint
Recall compact() ignores variables that do not exist, see key_moments explanation.
Concept Snapshot
compact('var1', 'var2', ...): creates an array with variable names as keys and their values.
extract(array): creates variables from array keys in current scope.
Use compact to pack variables, extract to unpack.
extract overwrites existing variables.
Useful for passing variables around easily.
Full Transcript
This example shows how PHP's compact() function takes variable names as strings and creates an array with those variables' values. Then extract() takes that array and creates variables in the current scope with the array's keys as variable names. The code defines $a=10 and $b=20, packs them into $arr with compact, then extracts them back. Finally, it prints the sum 30. The execution table traces each step, showing variable states and outputs. Key moments clarify common confusions about variable scope and behavior of these functions.