0
0
PHPprogramming~15 mins

Compact and extract functions in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Compact and Extract Functions in PHP
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use double quotes in echo to include variables directly.