Open In App
Related Articles

PHP Ds\Stack Functions Complete Reference

Improve Article
Save Article
Like Article

Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). The Ds\Stack uses Ds\Vector internally.

Requirements: PHP 7 is required for both extension and the compatibility polyfill.

Installation: The easiest way to install data structure by using the PECL extension.

pecl install ds


public Ds\Stack::functionName()

Example:  Below programs illustrate the Ds\Stack::pop() function in PHP:


// PHP program to illustrate the
// Ds\stack::pop() function
// Create a Stack instance
$stack = new \Ds\Stack();
// Pushing elements to Stack
// Print the initial Stack
// Print the top element and remove it
// Print the Stack again


Ds\Stack Object
    [0] => GfG
    [1] => to
    [2] => Welcome
Ds\Stack Object
    [0] => to
    [1] => Welcome

Complete list of data structure DS\Stack:



clear()Remove all elements from a Stack and clear it
copy()Create a shallow copy of the original stack and return the copied stack.
isEmpty()Check whether a Stack is empty or not. 
peek()Get the element present at the top of the Stack instance. 
pop()Remove the element present at the top of the Stack instance.
push()Add elements at the end of the stack
toArray()Convert the stack to an array and returns the converted array.
Last Updated : 25 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials