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?✗ Incorrect
array_pop() removes and returns the last element of the array.
Which function adds elements to the end of an array in PHP?
✗ Incorrect
array_push() adds elements to the end of an array.
What will
array_pop() return if the array is empty?✗ Incorrect
If the array is empty, array_pop() returns NULL.
How can you add multiple elements to an array at once using
array_push()?✗ Incorrect
You can pass multiple elements as additional arguments to array_push() to add them all at once.
Which of these is a correct way to add 'apple' and 'banana' to the array
$fruits?✗ Incorrect
array_push($fruits, 'apple', 'banana'); correctly adds both elements to the array.
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.