The compact() function creates an array from variables and their values. You give it variable names as strings, and it returns an associative array where keys are the variable names and values are the variable values.
Click to reveal answer
beginner
What is the purpose of the extract() function in PHP?
The extract() function imports variables into the current symbol table from an associative array. It creates variables named after the array keys and assigns them the corresponding values.
Click to reveal answer
intermediate
How do compact() and extract() relate to each other?
compact() turns variables into an array, while extract() turns an array back into variables. They are like opposites and can be used to pass variables around easily.
Click to reveal answer
intermediate
What happens if you use extract() with an array that has keys matching existing variables?
By default, extract() will overwrite existing variables with the same name. You can control this behavior with flags like EXTR_SKIP to avoid overwriting.
Click to reveal answer
beginner
Show a simple example of using compact() and extract() together.
Example:
$name = "Alice"; $age = 30; $data = compact('name', 'age'); // $data is ['name' => 'Alice', 'age' => 30] extract($data); // Now $name and $age variables exist again with original values
Click to reveal answer
What does compact('x', 'y') return if $x = 5 and $y = 10?
AVariables named 'x' and 'y' with values 5 and 10
BAn array [5, 10]
CAn array ['x' => 5, 'y' => 10]
DA string 'x y'
✗ Incorrect
compact() returns an associative array with keys as variable names and values as their values.
What does extract(['a' => 1, 'b' => 2]) do?
ADeletes variables <code>$a</code> and <code>$b</code>
BCreates variables <code>$a = 1</code> and <code>$b = 2</code>
CCreates an array with keys 'a' and 'b'
DReturns an array [1, 2]
✗ Incorrect
extract() creates variables named after the array keys with their corresponding values.
If you want to avoid overwriting existing variables when using extract(), which flag should you use?
AEXTR_OVERWRITE
BEXTR_IF_EXISTS
CEXTR_PREFIX_ALL
DEXTR_SKIP
✗ Incorrect
EXTR_SKIP skips extracting variables if they already exist, preventing overwriting.
Which function would you use to turn variables into an associative array?
Acompact()
Barray_keys()
Carray_merge()
Dextract()
✗ Incorrect
compact() creates an associative array from variables.
What will happen if you call extract() on an empty array?
ANothing happens, no variables are created
BIt throws an error
CIt creates variables with null values
DIt deletes all existing variables
✗ Incorrect
If the array is empty, extract() does nothing and no variables are created.
Explain how compact() and extract() work and how they can be used together.
Think about turning variables into arrays and back.
You got /4 concepts.
What precautions should you take when using extract() in your code?
Consider variable conflicts and safety.
You got /4 concepts.
Practice
(1/5)
1. What does the PHP function compact do?
easy
A. Creates an array from given variable names and their values
B. Extracts variables from an array into the current symbol table
C. Deletes variables from the current scope
D. Converts an array into a JSON string
Solution
Step 1: Understand the purpose of compact
The compact function 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 extract
extract does the opposite by creating variables from array keys, so it is not the correct description for compact.
Final Answer:
Creates an array from given variable names and their values -> Option A
Quick Check:
compact = array from variables [OK]
Hint: Remember: compact packs variables into an array [OK]
Common Mistakes:
Confusing compact with extract
Thinking compact extracts variables
Assuming compact converts arrays to strings
2. Which of the following is the correct syntax to use extract on an array $data?
easy
A. extract = $data;
B. extract($data);
C. extract->$data();
D. extract($data[]);
Solution
Step 1: Recall the function call syntax
In PHP, functions are called by their name followed by parentheses containing arguments. So extract($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 B
Quick Check:
Function call syntax = extract($data); [OK]
Hint: Use parentheses to call extract with array argument [OK]