What is Ds\Stack::pop() in PHP?

The Stack::pop() function in PHP returns the element that is available at the top of the stack and removes that element from the stack.

Figure 1 shows the visual representation of the Stack::pop() function.

Figure 1: Visual representation of Stack::pop() function

Syntax

stack_name->pop()
## where the stack_name is the name of the stack

Parameters

This function does not require any parameters.

Return value

This function returns the element that is available at the top of the stack and removes that element from the stack.

Example

<?php
$stack = new \Ds\Stack();
#sending the list of elements
$stack->push(1,3,5,2,0);
echo("Stack before pop");
print_r($stack);
echo("Stack after pop");
$stack->pop();
print_r($stack);
?>

Output

Stack before popDs\Stack Object
(
[0] => 0
[1] => 2
[2] => 5
[3] => 3
[4] => 1
)
Stack after popDs\Stack Object
(
[0] => 2
[1] => 5
[2] => 3
[3] => 1
)
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources