0
0
PHPprogramming~5 mins

Array push and pop in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the array_push() function do in PHP?
It adds one or more elements to the end of an array, increasing its size by the number of elements added.
Click to reveal answer
beginner
What does the array_pop() function do in PHP?
It removes the last element from an array and returns that element, reducing the array size by one.
Click to reveal answer
beginner
How do array_push() and array_pop() relate to a real-life stack of plates?
Adding a plate on top is like array_push(), and taking the top plate off is like array_pop(). Both work with the last item added.
Click to reveal answer
intermediate
What happens if you use array_pop() on an empty array?
It returns NULL because there is no element to remove.
Click to reveal answer
intermediate
Can array_push() add multiple elements at once? How?
Yes, by passing multiple values as additional arguments after the array, like array_push($arr, 'a', 'b', 'c').
Click to reveal answer
What does array_pop() return when called on a non-empty array?
AThe last element of the array
BThe first element of the array
CThe entire array
DNULL
Which function adds elements to the end of an array in PHP?
Aarray_push()
Barray_pop()
Carray_shift()
Darray_unshift()
What will array_pop() return if the array is empty?
AAn error
BNULL
CZero
DThe first element
How can you add multiple elements to an array at once using array_push()?
ABy calling <code>array_push()</code> multiple times
BBy using a loop inside <code>array_push()</code>
CBy passing multiple elements as arguments in one call
DYou cannot add multiple elements at once
Which of these is a correct way to add 'apple' and 'banana' to the array $fruits?
Aarray_pop('apple', 'banana', $fruits);
Barray_pop($fruits, 'apple', 'banana');
Carray_push('apple', 'banana', $fruits);
Darray_push($fruits, 'apple', 'banana');
Explain how array_push() and array_pop() work in PHP and give a real-life example.
Think about adding and removing plates from a stack.
You got /3 concepts.
    What happens if you call array_pop() on an empty array? Why is this behavior useful?
    Consider what happens when you try to take something from an empty box.
    You got /3 concepts.