0
0
PHPprogramming~10 mins

Compact and extract functions in PHP - Interactive Code Practice

Choose your learning style9 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.