Bird
Raised Fist0
PHPprogramming~5 mins

Array push and pop in PHP

Choose your learning style10 modes available
Introduction

We use push and pop to add or remove items from the end of a list. This helps us manage collections of things easily.

When you want to add a new item to the end of a list, like adding a new song to a playlist.
When you want to remove the last item from a list, like taking the last book off a stack.
When you need to keep track of things in order, adding and removing from the end only.
When you want to use a list like a stack, where the last thing added is the first to be removed.
Syntax
PHP
<?php
// Define an array
$array = [];

// Add item to the end
array_push($array, $item);

// Remove item from the end
$popped_item = array_pop($array);
?>

array_push adds one or more items to the end of the array.

array_pop removes and returns the last item from the array.

Examples
This adds 'apple' to an empty array, so the array now has one item.
PHP
<?php
// Example: Push items to an empty array
$array = [];
array_push($array, 'apple');
print_r($array);
?>
This removes 'apple' from the array and prints it. The array becomes empty.
PHP
<?php
// Example: Pop item from an array with one element
$array = ['apple'];
$popped = array_pop($array);
echo $popped . "\n";
print_r($array);
?>
This adds 'banana' and 'cherry' to the end of the array.
PHP
<?php
// Example: Push multiple items
$array = ['apple'];
array_push($array, 'banana', 'cherry');
print_r($array);
?>
Trying to pop from an empty array returns NULL.
PHP
<?php
// Example: Pop item from an empty array
$array = [];
$popped = array_pop($array);
var_dump($popped);
?>
Sample Program

This program starts with an empty array, adds three fruits, removes the last one, and prints the array before and after.

PHP
<?php
// Create an empty array
$fruits = [];

// Show initial state
echo "Initial array:\n";
print_r($fruits);

// Push items
array_push($fruits, 'apple');
array_push($fruits, 'banana', 'cherry');

echo "\nAfter pushing items:\n";
print_r($fruits);

// Pop an item
$popped_fruit = array_pop($fruits);
echo "\nPopped item: $popped_fruit\n";

// Show final state
echo "\nFinal array:\n";
print_r($fruits);
?>
OutputSuccess
Important Notes

Time complexity: Both array_push and array_pop work in constant time O(1).

Space complexity: array_push may increase array size, array_pop reduces it.

Common mistake: Forgetting that array_pop returns the removed item, so you can save it if needed.

Use push/pop when you only add or remove from the end. For other positions, use different functions.

Summary

array_push adds items to the end of an array.

array_pop removes and returns the last item from an array.

These functions help manage lists like stacks easily.

Practice

(1/5)
1. What does the PHP function array_push do to an array?
easy
A. Adds one or more elements to the end of the array
B. Removes the first element from the array
C. Removes the last element from the array
D. Sorts the array in ascending order

Solution

  1. Step 1: Understand the purpose of array_push

    array_push is used to add elements to the end of an existing array.
  2. Step 2: Compare with other array functions

    Functions like array_pop remove elements, but array_push only adds elements at the end.
  3. Final Answer:

    Adds one or more elements to the end of the array -> Option A
  4. Quick Check:

    array_push = add elements at end [OK]
Hint: Remember: push means add at the end [OK]
Common Mistakes:
  • Confusing array_push with array_pop
  • Thinking array_push removes elements
  • Assuming array_push sorts the array
2. Which of the following is the correct syntax to add an element 'apple' to the array $fruits using array_push?
easy
A. array_push('apple', $fruits);
B. array_push($fruits['apple']);
C. array_push($fruits => 'apple');
D. array_push($fruits, 'apple');

Solution

  1. Step 1: Recall the function signature

    The correct syntax is array_push(array, value1, value2, ...), where the first argument is the array variable.
  2. Step 2: Check each option

    array_push($fruits, 'apple'); correctly uses array_push($fruits, 'apple');. Others misuse argument order or syntax.
  3. Final Answer:

    array_push($fruits, 'apple'); -> Option D
  4. Quick Check:

    array_push(array, value) syntax [OK]
Hint: First argument is array, then values to add [OK]
Common Mistakes:
  • Swapping argument order
  • Using arrow => instead of comma
  • Passing only one argument incorrectly
3. What will be the output of the following PHP code?
$colors = ['red', 'green'];
array_push($colors, 'blue');
$last = array_pop($colors);
print_r($colors);
echo $last;
medium
A. Array ( [0] => red [1] => green [2] => blue ) blue
B. Array ( [0] => red [1] => green ) blue
C. Array ( [0] => red [1] => green ) green
D. Array ( [0] => red [1] => green [2] => blue ) green

Solution

  1. Step 1: Understand array_push effect

    array_push adds 'blue' to the end, so array becomes ['red', 'green', 'blue'].
  2. Step 2: Understand array_pop effect

    array_pop removes and returns the last element 'blue', so array becomes ['red', 'green'] and $last = 'blue'.
  3. Final Answer:

    Array ( [0] => red [1] => green ) blue -> Option B
  4. Quick Check:

    push adds, pop removes last [OK]
Hint: Push adds last, pop removes last [OK]
Common Mistakes:
  • Thinking pop removes first element
  • Expecting array to still have 'blue'
  • Confusing printed array and echoed value
4. Identify the error in the following PHP code snippet:
$numbers = [1, 2, 3];
array_pop($numbers, 2);
print_r($numbers);
medium
A. No error, code works fine
B. array_pop should be called before array initialization
C. array_pop accepts only one argument, extra argument causes error
D. array_pop cannot be used on numeric arrays

Solution

  1. Step 1: Check array_pop function signature

    array_pop accepts only one argument: the array variable.
  2. Step 2: Analyze the code

    Passing a second argument (2) is invalid and will cause an error.
  3. Final Answer:

    array_pop accepts only one argument, extra argument causes error -> Option C
  4. Quick Check:

    array_pop(array) only one argument [OK]
Hint: array_pop takes exactly one argument [OK]
Common Mistakes:
  • Passing multiple arguments to array_pop
  • Thinking array_pop removes multiple elements
  • Assuming no error on extra arguments
5. Given an empty array $stack = [];, which sequence of PHP statements correctly adds 'first', 'second', then removes the last added element and stores it in $removed?
hard
A. array_push($stack, 'first'); array_push($stack, 'second'); $removed = array_pop($stack);
B. $removed = array_pop($stack); array_push($stack, 'first'); array_push($stack, 'second');
C. array_pop($stack, 'first'); array_pop($stack, 'second'); $removed = array_push($stack);
D. array_push('first', $stack); array_push('second', $stack); $removed = array_pop($stack);

Solution

  1. Step 1: Add elements in correct order

    Use array_push($stack, 'first') and then array_push($stack, 'second') to add elements to the stack.
  2. Step 2: Remove last element and store it

    Use $removed = array_pop($stack); to remove and save the last added element ('second').
  3. Final Answer:

    array_push($stack, 'first'); array_push($stack, 'second'); $removed = array_pop($stack); -> Option A
  4. Quick Check:

    Push adds, pop removes last [OK]
Hint: Push then pop to simulate stack behavior [OK]
Common Mistakes:
  • Calling array_pop before pushing elements
  • Passing arguments in wrong order
  • Using array_pop with extra arguments
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