We use push and pop to add or remove items from the end of a list. This helps us manage collections of things easily.
Array push and pop in 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.
<?php // Example: Push items to an empty array $array = []; array_push($array, 'apple'); print_r($array); ?>
<?php // Example: Pop item from an array with one element $array = ['apple']; $popped = array_pop($array); echo $popped . "\n"; print_r($array); ?>
<?php // Example: Push multiple items $array = ['apple']; array_push($array, 'banana', 'cherry'); print_r($array); ?>
<?php // Example: Pop item from an empty array $array = []; $popped = array_pop($array); var_dump($popped); ?>
This program starts with an empty array, adds three fruits, removes the last one, and prints the array before and after.
<?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); ?>
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.
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
array_push do to an array?Solution
Step 1: Understand the purpose of
array_pusharray_pushis used to add elements to the end of an existing array.Step 2: Compare with other array functions
Functions likearray_popremove elements, butarray_pushonly adds elements at the end.Final Answer:
Adds one or more elements to the end of the array -> Option AQuick Check:
array_push = add elements at end [OK]
- Confusing array_push with array_pop
- Thinking array_push removes elements
- Assuming array_push sorts the array
$fruits using array_push?Solution
Step 1: Recall the function signature
The correct syntax isarray_push(array, value1, value2, ...), where the first argument is the array variable.Step 2: Check each option
array_push($fruits, 'apple'); correctly usesarray_push($fruits, 'apple');. Others misuse argument order or syntax.Final Answer:
array_push($fruits, 'apple'); -> Option DQuick Check:
array_push(array, value) syntax [OK]
- Swapping argument order
- Using arrow => instead of comma
- Passing only one argument incorrectly
$colors = ['red', 'green'];
array_push($colors, 'blue');
$last = array_pop($colors);
print_r($colors);
echo $last;
Solution
Step 1: Understand array_push effect
array_push adds 'blue' to the end, so array becomes ['red', 'green', 'blue'].Step 2: Understand array_pop effect
array_pop removes and returns the last element 'blue', so array becomes ['red', 'green'] and $last = 'blue'.Final Answer:
Array ( [0] => red [1] => green ) blue -> Option BQuick Check:
push adds, pop removes last [OK]
- Thinking pop removes first element
- Expecting array to still have 'blue'
- Confusing printed array and echoed value
$numbers = [1, 2, 3];
array_pop($numbers, 2);
print_r($numbers);
Solution
Step 1: Check array_pop function signature
array_pop accepts only one argument: the array variable.Step 2: Analyze the code
Passing a second argument (2) is invalid and will cause an error.Final Answer:
array_pop accepts only one argument, extra argument causes error -> Option CQuick Check:
array_pop(array) only one argument [OK]
- Passing multiple arguments to array_pop
- Thinking array_pop removes multiple elements
- Assuming no error on extra arguments
$stack = [];, which sequence of PHP statements correctly adds 'first', 'second', then removes the last added element and stores it in $removed?Solution
Step 1: Add elements in correct order
Usearray_push($stack, 'first')and thenarray_push($stack, 'second')to add elements to the stack.Step 2: Remove last element and store it
Use$removed = array_pop($stack);to remove and save the last added element ('second').Final Answer:
array_push($stack, 'first'); array_push($stack, 'second'); $removed = array_pop($stack); -> Option AQuick Check:
Push adds, pop removes last [OK]
- Calling array_pop before pushing elements
- Passing arguments in wrong order
- Using array_pop with extra arguments
Ready to prove it?
Pass the test and this pattern moves unknown
