📖 Scenario: You are building a simple PHP script to manage user information. You want to group user details into an array easily and then extract them back into variables for use.
🎯 Goal: Learn how to use the compact function to create an array from variables and the extract function to convert array keys back into variables.
📋 What You'll Learn
Create variables with exact names and values as specified
Use compact to create an array from these variables
Use extract to convert the array back into variables
Print the extracted variables exactly as instructed
💡 Why This Matters
🌍 Real World
Grouping related data into arrays and extracting them as variables is common in PHP web development, especially when handling form data or database results.
💼 Career
Understanding <code>compact</code> and <code>extract</code> helps in reading and writing clean PHP code, useful for backend web developer roles.
Progress0 / 4 steps
1
Create user variables
Create three variables: $name with value "Alice", $age with value 30, and $city with value "New York".
PHP
Hint
Use the = operator to assign values to variables.
2
Create an array using compact
Use the compact function to create an array called $userInfo from the variables $name, $age, and $city.
PHP
Hint
Pass the variable names as strings to compact.
3
Extract variables from the array
Use the extract function to create variables from the $userInfo array.
PHP
Hint
Call extract with the array variable as the argument.
4
Print the extracted variables
Print the variables $name, $age, and $city separated by commas using echo.
PHP
Hint
Use double quotes in echo to include variables directly.
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]