Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is Ds\Stack::push() 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::push() function in PHP is used to insert an element or list of elements at the top of the stack.

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

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

Syntax

stack_name->push(element(s))
## where the stack_name is the name of the stack

Parameter

This function requires an element or list of elements separated by commas as a parameter.

The elements can be of mixed data type.

Return value

This function inserts the element or list of elements sent as a parameter at the top of the stack.

Example

<?php
$stack = new \Ds\Stack();
#sending the list of elements
$stack->push(1,3,5,2);
echo("Stack before 0");
print_r($stack);
#sending single element of different data type
$stack->push("zero");
echo("Stack after 0");
print_r($stack);
?>

Output

Stack before 0Ds\Stack Object
(
[0] => 2
[1] => 5
[2] => 3
[3] => 1
)
Stack after 0Ds\Stack Object
(
[0] => zero
[1] => 2
[2] => 5
[3] => 3
[4] => 1
)

RELATED TAGS

php
communitycreator

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