{"id":334,"date":"2020-11-15T14:58:05","date_gmt":"2020-11-15T14:58:05","guid":{"rendered":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/how-can-i-implement-stack-in-php\/"},"modified":"2024-10-28T04:31:08","modified_gmt":"2024-10-28T04:31:08","slug":"how-can-i-implement-stack-in-php","status":"publish","type":"programming_with_php","link":"https:\/\/demo.wpartisan.net\/developmentsite\/programming-with-php\/how-can-i-implement-stack-in-php\/","title":{"rendered":"How Can I Implement Stack in PHP?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A Stack is a linear data structure that follows a particular order in which the operations are performed. This order is called LIFO(Last In, First Out). The element inserted last is the first element to be popped.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Stack, insertion and deletion operations are called push and pop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can implement it in both a procedural or object-oriented approach. Here I follow the procedural process.<\/p>\n\n\n\n<pre class=\"wp-block-code \\&quot;wp-block-code\\&quot;\"><code lang=\"\\&quot;php\\&quot;\" class=\"language-\\&quot;php\\&quot;\">$data_array = [];\n$top = -1;\n\nfunction push($top, $item, $data_array){\n    global $top,$data_array;\n    $top++;\n    $data_array[$top] = $item;\n    return $data_array;\n}\n\n\nfunction pop(){\n    global $top,$data_array;\n    if($top &lt; 0)\n        return -1;\n    $top_item = $data_array[$top];\n    unset($data_array[$top]);\n    $top--;\n    return $top_item;\n}\npush($top, 1, $data_array);\npush($top, 2, $data_array);\npush($top, 3, $data_array)\n\npop();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Stack is a linear data structure that follows a particular order in which the operations are performed. The order is LIFO(Last In, First Out). We can implement it in both a procedural or object-oriented approach. Here I follow the procedural process.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}}},"class_list":["post-334","programming_with_php","type-programming_with_php","status-publish","hentry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"WPArtisan","author_link":"https:\/\/demo.wpartisan.net\/developmentsite\/author\/developmentsiteauth\/"},"uagb_comment_info":0,"uagb_excerpt":"A Stack is a linear data structure that follows a particular order in which the operations are performed. The order is LIFO(Last In, First Out). We can implement it in both a procedural or object-oriented approach. Here I follow the procedural process.","_links":{"self":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/programming_with_php\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/programming_with_php"}],"about":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/types\/programming_with_php"}],"author":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/comments?post=334"}],"wp:attachment":[{"href":"https:\/\/demo.wpartisan.net\/developmentsite\/wp-json\/wp\/v2\/media?parent=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}