Current Location: Home> Latest Articles> Use end() to implement peek operations in stack structure

Use end() to implement peek operations in stack structure

M66 2025-06-05

In PHP, Stack is a data structure that is first-in-and-out (LIFO, Last In First Out). In the stack, the addition and removal of elements are performed from the top of the stack. The stack is often used to implement some scenarios that require late-in-first-out (LIFO) logic, such as undo operations, browsing history, etc. Although PHP does not have a built-in stack data structure, we can use arrays to simulate stack operations.

Among the common operations of the stack, there is a common requirement: view the top element of the stack , but do not delete it. This is called peek operation. For PHP arrays, although we can use the array_pop() function to pop up the top element, the purpose of peek operation is to only view the top element of the stack without changing the state of the stack itself.

There is a very useful function in PHP called end() which returns the last element in the array. We can use it to implement peek operations on the stack. Let's take a look at the specific implementation method.

Basic implementation of stack

We first simulate a simple stack, using PHP arrays to simulate the operation of the stack.

 <?php
// Initialize the stack
$stack = [];

// Stack operation
array_push($stack, 'first');
array_push($stack, 'second');
array_push($stack, 'third');

// The status of the output stack
print_r($stack);
?>

Run the code and the output will be:

 Array
(
    [0] => first
    [1] => second
    [2] => third
)

At this time, the top element of the stack is 'third' .

How to implement Peek operations?

To implement peek operations, we can use the end() function. The end() function returns the last element of the array, that is, the top element of the stack.

 <?php
// Peek operate:View the top element of the stack
$top = end($stack);

// Output stack top element
echo "The top element of the stack is: " . $top . PHP_EOL;
?>

The output is:

 The top element of the stack is: third

Why use end() ?

The end() function moves the inner pointer of the array to the last element, but does not delete the element. Therefore, the stack remains unchanged after performing the peek operation. This feature is very suitable for peek operations in stack structures, because we only need to view the elements at the top of the stack, and do not want to modify the stack.

Things to note

  1. The case of empty stack : If the stack is empty, end() returns false . Therefore, before using end() , it is best to check whether the stack is empty to avoid errors.

 <?php
if (!empty($stack)) {
    $top = end($stack);
    echo "The top element of the stack is: " . $top . PHP_EOL;
} else {
    echo "The stack is empty,Unable to execute peek operate。" . PHP_EOL;
}
?>
  1. Internal pointer of the array : The end() function changes the internal pointer of the array, but for stack operations, the change of the internal pointer will not affect other operations of the stack (such as stack entry, stack exit, etc.).

Complete code example

Here is a complete example code that includes stack entry, stack exit, and peek operations:

 <?php
// Initialize the stack
$stack = [];

// Stack operation
array_push($stack, 'first');
array_push($stack, 'second');
array_push($stack, 'third');

// Peek operate:View the top element of the stack
if (!empty($stack)) {
    $top = end($stack);
    echo "The top element of the stack is: " . $top . PHP_EOL;
} else {
    echo "The stack is empty,Unable to execute peek operate。" . PHP_EOL;
}

// 出栈operate:Remove top element of stack
$removed = array_pop($stack);
echo "The removed element is: " . $removed . PHP_EOL;

// Peek operate:再次View the top element of the stack
if (!empty($stack)) {
    $top = end($stack);
    echo "The top element of the stack is: " . $top . PHP_EOL;
} else {
    echo "The stack is empty,Unable to execute peek operate。" . PHP_EOL;
}
?>

Output result:

 The top element of the stack is: third
The removed element is: third
The top element of the stack is: second

Summarize

By using the end() function, we can easily implement the peek operation of the stack, viewing the elements on the top of the stack without changing the state of the stack. This method is simple and efficient, and is very suitable for scenarios that simulate stack data structures in PHP.