Open In App

PHP Ds\Stack Functions Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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

Syntax:

public Ds\Stack::functionName()

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

PHP




<?php
 
// PHP program to illustrate the
// Ds\stack::pop() function
 
// Create a Stack instance
$stack = new \Ds\Stack();
 
// Pushing elements to Stack
$stack->push("Welcome");
$stack->push("to");
$stack->push("GfG");
 
// Print the initial Stack
print_r($stack);
 
// Print the top element and remove it
print_r($stack->pop());
 
// Print the Stack again
print_r($stack);
 
?>


Output:

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

Complete list of data structure DS\Stack:

 Functions

Description

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
Previous
Next
Share your thoughts in the comments
Similar Reads