From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Using SplStack and SplQueue

Using SplStack and SplQueue - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Using SplStack and SplQueue

- SplStack and SplQueue are specialized versions of WLinkLists. SplStack works on the basis of last in first out while SplQueue processes values on a first in first out basis. This is stack_queue.php which you can find in the chapter seven folder of the exercise files. The code on lines two to seven creates an instance of SplStack and populates it with the letters A to E. It uses a mixture of bracket notation and the push method. With bracket notation, you assign the value in the same way as you would with an ordinary array. With push method, you pass the value as an argument. It doesn't matter which you choose. Lines nine to 14, create an instance of SplQueue which is also populated with the letters A to E. On lines 10 and 11, the enqueue method is used and on line 12, the push method and lines 13 and 14, use bracket notation. They all do exactly the same thing, they add items to the list. Then we've got two for each loops that display the contents of the stack and queue. Let's load…

Contents