A sequential collection of elements with a particular property associated with them is called a stack in PHP. And a stack operates on a Last In First Out basis, which means the object that is placed at last in the stack will be the first object to be removed from the stack, and the addition of elements and deletion of elements to the stack is all restricted to only one end of the stack.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The syntax to declare stack in PHP is as follows:
push(item_to_added_to_the_stack); pop();
where item_to_be_added_to_the_stack is the item that will be added to the stack from the top of the stack.
Working of Stack in PHP is as follows:
Let us discuss examples of PHP Stack Trace.
PHP program to add the items to a stack and delete the items from the stack from the top of the stack using the push() function and pop() function and then display the contents of the stack:
Code:
<?php //creating an instance of SplQueue class $newstack = new SplQueue(); //using push() function to the add items to the stack from the top of the stack $newstack->push('Welcome'); $newstack->push('to'); $newstack->push('PHP'); //printing the contents of the stack after push operation in a human readable format by using print_r function echo "The elements present in the stack after push operation are:\n"; print_r ($newstack); //Removing two items from the top of the stack using pop() function and then displaying the contents of the stack in human readable form using print_r function $newstack->pop(); $newstack->pop(); echo "The elements present in the stack after pop operation are:\n"; print_r ($newstack); ?>
Output:
Then we are using push() operation to add the elements to the stack from the top of the stack. Then we are displaying the contents of the stack as the output on the screen. Then we are using the pop() operation to remove the elements from the stack from the top of the stack. Then we are displaying the contents of the stack as the output on the screen.
PHP program to add the items to a stack and delete the items from the stack from the top of the stack using the push() function and pop() function and then display the contents of the stack:
Code:
<?php //creating an instance of SplQueue class $newstack = new SplQueue(); //using push() function to the add items to the stack from the top of the stack $newstack->push('Learning'); $newstack->push('is'); $newstack->push('fun'); //printing the contents of the stack after push operation in a human readable format by using print_r function echo "The elements present in the stack after push operation are:\n"; print_r ($newstack); //Removing two items from the top of the stack using pop() function and then displaying the contents of the stack in human readable form using print_r function $newstack->pop(); $newstack->pop(); $newstack->pop(); echo "The elements present in the stack after pop operation are:\n"; print_r ($newstack); ?>
Output:
Then we are using push() operation to add the elements to the stack from the top of the stack. Then we are displaying the contents of the stack as the output on the screen. Then we are using the pop() operation to remove the elements from the stack from the top of the stack. Then we are displaying the contents of the stack as the output on the screen.
PHP program to add the items to a stack and delete the items from the stack from the top of the stack using the push() function and pop() function and then display the contents of the stack:
Code:
<?php //creating an instance of SplQueue class $newstack = new SplQueue(); //using push() function to the add items to the stack from the top of the stack $newstack->push('We'); $newstack->push('love'); $newstack->push('India'); //printing the contents of the stack after push operation in a human readable format by using print_r function echo "The elements present in the stack after push operation are:\n"; print_r ($newstack); //Removing two items from the top of the stack using pop() function and then displaying the contents of the stack in human readable form using print_r function $newstack->pop(); echo "The elements present in the stack after pop operation are:\n"; print_r ($newstack); ?>
Output:
Then we are using push() operation to add the elements to the stack from the top of the stack. Then we are displaying the contents of the stack as the output on the screen. Then we are using the pop() operation to remove the elements from the stack from the top of the stack. Then we are displaying the contents of the stack as the output on the screen.
In this article, we have learned the concept of a stack in PHP through definition, syntax, and basic operations that define a stack, namely the push() function and pop() function in PHP through programming examples and their outputs.
The above is the detailed content of PHP Stack Trace. For more information, please follow other related articles on the PHP Chinese website!