Bird
Raised Fist0
PHPprogramming~10 mins

Compact and extract functions in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an array from variables using the compact function.

PHP
<?php
$name = "Alice";
$age = 25;
$data = [1]("name", "age");
print_r($data);
?>
Drag options to blanks, or click blank then click option'
Aextract
Bcompact
Carray
Dimplode
Attempts:
3 left
💡 Hint
Common Mistakes
Using extract instead of compact.
Trying to use array() with variable names as strings.
2fill in blank
medium

Complete the code to extract variables from the array using the extract function.

PHP
<?php
$data = ['city' => 'Paris', 'country' => 'France'];
[1]($data);
echo $city . ', ' . $country;
?>
Drag options to blanks, or click blank then click option'
Aimplode
Bcompact
Carray_merge
Dextract
Attempts:
3 left
💡 Hint
Common Mistakes
Using compact instead of extract.
Trying to echo array elements without extracting.
3fill in blank
hard

Fix the error in the code by choosing the correct function to create an array from variables.

PHP
<?php
$fruit = "apple";
$color = "red";
$info = [1]("fruit", "color");
print_r($info);
?>
Drag options to blanks, or click blank then click option'
Acompact
Bimplode
Carray_push
Dextract
Attempts:
3 left
💡 Hint
Common Mistakes
Using extract instead of compact.
Using implode which joins array elements into a string.
4fill in blank
hard

Fill both blanks to create an array from variables and then extract them back.

PHP
<?php
$animal = "dog";
$legs = 4;
$data = [1]("animal", "legs");
[2]($data);
echo $animal . ' has ' . $legs . ' legs.';
?>
Drag options to blanks, or click blank then click option'
Acompact
Bimplode
Cextract
Darray_merge
Attempts:
3 left
💡 Hint
Common Mistakes
Using implode instead of compact.
Using array_merge instead of extract.
5fill in blank
hard

Fill both blanks to create an array from variables, extract them, and print a sentence.

PHP
<?php
$brand = "Toyota";
$model = "Corolla";
$year = 2020;
$data = [1]("brand", "model", "year");
[2]($data);
echo "Car: " . $brand . " " . $model . " (" . $year . ")";
?>
Drag options to blanks, or click blank then click option'
Aextract
Bcompact
Cprint_r
Dimplode
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up compact and extract order.
Using implode which joins array elements into a string.

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

  1. 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.
  2. Step 2: Differentiate from extract

    extract does the opposite by creating variables from array keys, so it is not the correct description for compact.
  3. Final Answer:

    Creates an array from given variable names and their values -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify incorrect syntax

    Options B, C, and D misuse assignment, object operator, or array syntax, which are invalid for function calls.
  3. Final Answer:

    extract($data); -> Option B
  4. Quick Check:

    Function call syntax = extract($data); [OK]
Hint: Use parentheses to call extract with array argument [OK]
Common Mistakes:
  • Using assignment instead of function call
  • Using object operator (->) incorrectly
  • Adding brackets after variable name
3. What is the output of the following PHP code?
$name = "Alice";
$age = 30;
$arr = compact('name', 'age');
print_r($arr);
medium
A. Array ( [Alice] => name [30] => age )
B. Array ( [0] => name [1] => age )
C. Error: Undefined variables
D. Array ( [name] => Alice [age] => 30 )

Solution

  1. Step 1: Understand compact behavior

    The compact function creates an array where keys are the variable names as strings and values are the current values of those variables.
  2. Step 2: Apply to given variables

    Variables $name and $age have values "Alice" and 30, so the array will be ['name' => 'Alice', 'age' => 30].
  3. Final Answer:

    Array ( [name] => Alice [age] => 30 ) -> Option D
  4. Quick Check:

    compact('name','age') = array with keys and values [OK]
Hint: compact creates array with variable names as keys [OK]
Common Mistakes:
  • Thinking compact returns variable values as keys
  • Expecting numeric keys instead of variable names
  • Assuming compact prints variables directly
4. Identify the error in this PHP code snippet:
$data = ['color' => 'red', 'size' => 'large'];
extract($data);
echo $colour;
medium
A. Variable name mismatch: $colour vs 'color' key
B. extract() cannot be used on associative arrays
C. Missing semicolon after extract()
D. echo cannot print variables created by extract()

Solution

  1. Step 1: Check variable names created by extract()

    The extract function creates variables named exactly as the array keys, so it creates $color and $size.
  2. Step 2: Compare echoed variable name

    The code tries to echo $colour which is different from $color, causing an undefined variable notice.
  3. Final Answer:

    Variable name mismatch: $colour vs 'color' key -> Option A
  4. Quick Check:

    extract keys = variable names; spelling must match [OK]
Hint: Check spelling of variable names after extract [OK]
Common Mistakes:
  • Assuming extract changes variable names
  • Thinking extract only works on indexed arrays
  • Ignoring PHP notices for undefined variables
5. Given the array $info = ['name' => 'Bob', 'age' => 25, 'city' => 'Paris', 'age' => 30];, what will be the result of extract($info); echo $age;?
hard
A. 25
B. Error: Duplicate keys in array
C. 30
D. Undefined variable error

Solution

  1. 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.
  2. Step 2: Apply extract() and echo

    Extract creates variables from keys, so $age will be 30, the last value. Echoing $age prints 30.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    Duplicate keys keep last value; extract uses that [OK]
Hint: Last duplicate key value is used in arrays [OK]
Common Mistakes:
  • Expecting first duplicate key value to be used
  • Thinking extract throws error on duplicates
  • Assuming undefined variable error due to duplicates
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard