0
0
PHPprogramming~10 mins

Array push and pop 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 add an element to the end of the array.

PHP
<?php
$fruits = ['apple', 'banana'];
array_[1]($fruits, 'orange');
print_r($fruits);
?>
Drag options to blanks, or click blank then click option'
Apop
Bshift
Cpush
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_pop instead of array_push, which removes elements.
Using array_shift or array_unshift which work on the start of the array.
2fill in blank
medium

Complete the code to remove the last element from the array and store it in a variable.

PHP
<?php
$colors = ['red', 'green', 'blue'];
$lastColor = array_[1]($colors);
echo $lastColor;
?>
Drag options to blanks, or click blank then click option'
Ashift
Bpush
Cunshift
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_push which adds elements instead of removing.
Using array_shift which removes from the start.
3fill in blank
hard

Fix the error in the code to correctly add 'pear' to the fruits array.

PHP
<?php
$fruits = ['apple', 'banana'];
array_[1]($fruits, 'pear');
print_r($fruits);
?>
Drag options to blanks, or click blank then click option'
Apop
Bpush
Cshift
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of arguments causes errors or unexpected behavior.
4fill in blank
hard

Fill both blanks to add 'kiwi' and then remove the last element from the array.

PHP
<?php
$fruits = ['apple', 'banana'];
array_[1]($fruits, 'kiwi');
$removed = array_[2]($fruits);
echo $removed;
?>
Drag options to blanks, or click blank then click option'
Apush
Bpop
Cshift
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_pop before array_push changes the array unexpectedly.
Using shift or unshift instead of push/pop.
5fill in blank
hard

Fill all three blanks to add 'mango', remove the last element, and then add 'grape'.

PHP
<?php
$fruits = ['apple', 'banana'];
array_[1]($fruits, 'mango');
$removed = array_[2]($fruits);
array_[3]($fruits, 'grape');
print_r($fruits);
?>
Drag options to blanks, or click blank then click option'
Apush
Bpop
Cshift
Dunshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using shift or unshift which affect the start of the array.
Mixing up the order of operations.