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
221: Versioning Content Revisions
When you first decide to implement "Undo" or "Revision History" in your PHP application, your instinct is probably to keep everything in one place. You've likely thought: "I'll just add a version column to my articles table. Every time someone hits save, I'll just insert a new row with an incremented version number."
The Single-Table Versioning Trap
It sounds clean, but in practice, it's a performance nightmare. Let's look at why. Imagine you have 1,000 articles, and each has an average of 10 revisions. Your table now has 10,000 rows. Every time you want to display your "Latest Articles" list on the homepage, your query looks something like this:
SELECT * FROM articles
WHERE id IN (SELECT MAX(id) FROM articles GROUP BY original_post_id);
I've seen this approach crash production databases. As the history grows, the "simple" act of fetching the current version of a page requires the database to scan through thousands of obsolete rows just to find the one that actually matters. You're forcing the DB to do a massive amount of work for the 99% of your users who don't care about the history—they just want to read the current post.
Decoupling Current State from History
The professional way to handle this is to separate the Current State from the Audit Trail. You need two tables: one for the "Live" content and one for the "Revisions."
The articles table stays lean. It holds the current title, current content, and the current version_number. The article_revisions table is where the bulk of the data lives. It's essentially a write-only archive until someone explicitly asks to see a previous version.
Here is the logic I typically use when saving a revision:
- Step A: Fetch the current record from
articles. - Step B: Insert that current record into
article_revisions. This ensures the "old" version is safely archived before it's gone. - Step C: Update the
articlestable with the new content and increment theversion_number.
In PHP, your save logic would look something like this:
// Assume $db is your PDO instance and $postId is the article being edited
$current = $db->prepare("SELECT * FROM articles WHERE id = ?");
$current->execute([$postId]);
$article = $current->fetch();
// 1. Archive the current version before overwriting it
$archive = $db->prepare("INSERT INTO article_revisions (article_id, content, version) VALUES (?, ?, ?)");
$archive->execute([$article['id'], $article['content'], $article['version']]);
// 2. Update the live table with new content
$update = $db->prepare("UPDATE articles SET content = ?, version = version + 1 WHERE id = ?");
$update->execute([$_POST['content'], $postId]);
Notice how this keeps your read queries lightning fast. When you want to show the article to a visitor, you just SELECT * FROM articles WHERE id = ?. No joins, no subqueries, and no scanning through ten years of typos and edits. You only touch the article_revisions table when the user clicks "View History" or "Restore this Version."
One last tip: don't version everything. If you have a last_edited_by or view_count column, don't put those in the revisions table. Only archive the fields that actually represent the content of the work. There's no reason to store 50 copies of a view counter.
📋 Practical Task
Build a "Restore to Version" Logic for a Wiki Page
You have two tables: wiki_pages (id, title, content, version) and wiki_revisions (id, page_id, content, version).
Your task is to write a PHP function restorePageVersion($db, $pageId, $versionToRestore). This function must:
- Verify that the requested version actually exists in the
wiki_revisionstable for that specificpage_id. - If it exists, take the
contentfrom that revision and update thewiki_pagestable. - Increment the
versionnumber in thewiki_pagestable (because restoring a version is, in itself, a new edit). - Return
trueon success orfalseif the version was not found.
Constraints: Use PDO for all database interactions to prevent SQL injection. Ensure you handle the case where a user tries to restore a version that doesn't exist without crashing the script.
There are no comments for now.