PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
58: Implementing a Stack and Queue
Imagine you're working in a busy cafeteria. You've got a stack of clean plates sitting on the counter. When you add a new plate, you put it right on top of the pile. When a customer needs a plate, they take the one from the top. The last plate you put down is the very first one that gets taken. That's a Stack.
Now, imagine you're standing in line at a coffee shop. You're the first person in line, so you're the first person the barista serves. The person who arrives last has to wait until everyone in front of them is gone. That's a Queue.
In PHP, we can implement both of these using arrays, but the magic lies in which array functions we choose. While you could just use raw arrays everywhere, wrapping these in classes makes your code much more predictable and prevents some of the weird bugs that happen when you accidentally treat a queue like a stack.
Building a LIFO Stack for "Undo" Operations
Since a stack is Last-In, First-Out (LIFO), we only care about one end of the array: the end. I like to think of this as the "top" of the stack. In PHP, array_push() and array_pop() are your best friends here. They both operate on the end of the array, which is incredibly efficient.
class UndoStack {
private array $history = [];
public function pushAction(string $action): void {
// We add the action to the top of the stack
array_push($this->history, $action);
}
public function undo(): ?string {
if ($this->isEmpty()) {
return null;
}
// The last action pushed is the first one popped off
return array_pop($this->history);
}
public function isEmpty(): bool {
return empty($this->history);
}
}
// Let's see it in action
$editor = new UndoStack();
$editor->pushAction("Typed 'Hello'");
$editor->pushAction("Typed ' World'");
$editor->pushAction("Changed font to Bold");
echo $editor->undo(); // Outputs: Changed font to Bold
echo $editor->undo(); // Outputs: Typed ' World'
Managing a FIFO Queue for Background Tasks
A queue is First-In, First-Out (FIFO). We still add items to the end, but we take them from the front. To do this in PHP, we use array_push() to enqueue and array_shift() to dequeue. array_shift() is the key here—it pulls the first element off and re-indexes the rest of the array.
One quick heads-up: array_shift() is slower than array_pop() because PHP has to move every other element in the array down one spot. For a few hundred items, you won't notice. For a million? You'll want a different data structure, like SplQueue. But for most day-to-day logic, this array approach is the standard way to go.
class PrintJobQueue {
private array $jobs = [];
public function enqueue(string $documentName): void {
// Add to the end of the line
array_push($this->jobs, $documentName);
}
public function dequeue(): ?string {
if ($this->isEmpty()) {
return null;
}
// Remove from the front of the line
return array_shift($this->jobs);
}
public function isEmpty(): bool {
return empty($this->jobs);
}
}
$printer = new PrintJobQueue();
$printer->enqueue("Quarterly_Report.pdf");
$printer->enqueue("Vacation_Photo.jpg");
$printer->enqueue("Resume_Final_v2.docx");
echo $printer->dequeue(); // Outputs: Quarterly_Report.pdf
echo $printer->dequeue(); // Outputs: Vacation_Photo.jpg
📋 Practical Task
Build a Browser History Navigator
Your task is to create a system that mimics how a web browser handles the "Back" button. You will need to implement two stacks: a currentHistoryStack and a forwardHistoryStack.
Requirements:
- Create a class
BrowserNavigator. - Implement a
visit(string $url)method: This should push the new URL onto thecurrentHistoryStackand clear theforwardHistoryStack(because once you visit a new page, you can't go "forward" anymore). - Implement a
goBack()method: This should pop the current page from thecurrentHistoryStackand push it onto theforwardHistoryStack, returning the new top of the current stack. - Implement a
goForward()method: This should pop from theforwardHistoryStackand push it back onto thecurrentHistoryStack.
Test your implementation by visiting three different URLs, going back twice, visiting a new URL, and then attempting to go forward (which should now be impossible).
There are no comments for now.