0
0
PHPprogramming~20 mins

Array push and pop in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Push and Pop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code using array_push and array_pop?

Consider the following PHP code:

$fruits = ['apple', 'banana'];
array_push($fruits, 'cherry');
$popped = array_pop($fruits);
echo $popped . ', ' . implode(', ', $fruits);

What will be printed?

PHP
$fruits = ['apple', 'banana'];
array_push($fruits, 'cherry');
$popped = array_pop($fruits);
echo $popped . ', ' . implode(', ', $fruits);
Acherry, apple, banana
Bcherry, apple, banana, cherry
Cherry, apple, banana
Dananab ,elppa ,yrrehc
Attempts:
2 left
💡 Hint

Remember that array_pop removes the last element and returns it.

Predict Output
intermediate
2:00remaining
What is the final array after these push and pop operations?

Given this PHP code:

$numbers = [1, 2, 3];
array_push($numbers, 4);
array_pop($numbers);
array_push($numbers, 5);
array_pop($numbers);
array_push($numbers, 6);
print_r($numbers);

What will print_r output?

PHP
$numbers = [1, 2, 3];
array_push($numbers, 4);
array_pop($numbers);
array_push($numbers, 5);
array_pop($numbers);
array_push($numbers, 6);
print_r($numbers);
A
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
B
Array
(
    [0] => 6
)
C
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
)
D
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 6
)
Attempts:
2 left
💡 Hint

Each array_pop removes the last element added.

Predict Output
advanced
2:00remaining
What does this PHP code output when mixing array_push with multiple values?

Look at this PHP code:

$colors = ['red'];
array_push($colors, 'green', 'blue');
$popped = array_pop($colors);
echo $popped . ', ' . implode(', ', $colors);

What will be printed?

PHP
$colors = ['red'];
array_push($colors, 'green', 'blue');
$popped = array_pop($colors);
echo $popped . ', ' . implode(', ', $colors);
Ablue, red, green
Bgreen, red, blue
Cblue, red, green, blue
Dgreen, red
Attempts:
2 left
💡 Hint

array_push can add multiple elements at once. array_pop removes the last element.

Predict Output
advanced
2:00remaining
What error or output does this PHP code produce?

Analyze this PHP code:

$items = [];
$popped = array_pop($items);
echo $popped === null ? 'null' : $popped;

What will be printed?

PHP
$items = [];
$popped = array_pop($items);
echo $popped === null ? 'null' : $popped;
AWarning: array_pop() expects parameter 1 to be array, null given
Bnull
CEmpty string
D0
Attempts:
2 left
💡 Hint

What happens when you pop from an empty array?

🧠 Conceptual
expert
2:00remaining
How many elements are in the array after this PHP code runs?

Consider this PHP code snippet:

$stack = [];
array_push($stack, 'a');
array_push($stack, 'b', 'c');
array_pop($stack);
array_push($stack, 'd');
array_pop($stack);
array_pop($stack);

How many elements remain in $stack after all operations?

A3
B2
C1
D0
Attempts:
2 left
💡 Hint

Count carefully each push and pop step.