Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator
stack

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

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Stack::peek() function in PHP is used to get the reference of the element at the top of a stack.

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

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

Syntax

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

Parameter

This function does not require a parameter.

Return value

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

It does not remove 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 top element: ");
print_r($stack->peek());
?>

Output

Stack top element: 0

RELATED TAGS

php
communitycreator
stack

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring