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
105: serialize and unserialize
I once spent three hours debugging a production issue where a user's settings were suddenly disappearing. The developer before me had stored a PHP array in a database column using serialize(). To "quickly" update a username in the database via a SQL query, they had manually edited the string. The result? The unserialize() call started returning false, and the application just wiped the user's preferences because it thought the data was missing.
The Mystery of the Vanishing Data
<?php
// Imagine this string was pulled from a database column
// It represents an array: ['username' => 'john']
$stored_data = 'a:1:{s:8:"username";s:4:"john";}';
$user_settings = unserialize($stored_data);
var_dump($user_settings);
// Result: array(1) { ["username"]=> string(4) "john" }
// Now, imagine a developer manually edited the database to change "john" to "jonathan"
// but forgot to update the length prefix (s:4)
$corrupted_data = 'a:1:{s:8:"username";s:4:"jonathan";}';
$user_settings = unserialize($corrupted_data);
var_dump($user_settings);
// Result: bool(false)
?>
If you run this, you'll notice that the second call doesn't throw a loud error; it just returns false. In a real app, a developer might write if (!$user_settings) { $user_settings = []; }, effectively deleting the user's data because of a single digit mistake in a string.
How PHP Tracks Serialized Strings
The reason this fails is that PHP's serialize() format isn't just a way to represent data; it's a strict map. Let's look at s:4:"john". The s stands for string, and the 4 tells PHP exactly how many bytes to read following the colon.
When PHP encountered s:4:"jonathan", it read exactly four characters ("jona") and then expected to find a closing quote. Instead, it found "than". This mismatch makes the entire string "malformed," and unserialize() gives up immediately. I've seen this happen countless times when people try to "hack" a database value without using the actual PHP functions.
Using Serialization the Right Way
To avoid this, you should never manually edit a serialized string. If you need to change a value, you must unserialize it, modify the resulting PHP variable, and then re-serialize it.
<?php
$data = 'a:1:{s:8:"username";s:4:"john";}';
$settings = unserialize($data);
// Modify the value using standard array logic
$settings['username'] = 'jonathan';
// Let PHP handle the length calculations
$updated_data = serialize($settings);
echo $updated_data;
// Result: a:1:{s:8:"username";s:8:"jonathan";}
?>
Now the string is s:8:"jonathan", and PHP is happy. As a side note: if you find yourself needing to edit these values manually or share them with a JavaScript frontend, stop using serialize() and use json_encode() and json_decode() instead. JSON is a standard across languages and doesn't rely on these fragile length-prefixes. However, serialize() remains useful when you need to preserve specific PHP object types, which JSON cannot do.
📋 Practical Task
Building a User Preference Snapshot System
You are building a feature that allows users to "save" a snapshot of their current dashboard layout (which is stored as an associative array) so they can restore it later. This snapshot needs to be stored as a single string in a database.
Your Task:
- Create an associative array called
$dashboard_layoutwith at least three keys (e.g.,'theme','sidebar_collapsed', and'widgets'—where widgets is itself a nested array of strings). - Use
serialize()to convert this array into a string and store it in a variable called$db_snapshot. - Simulate a "restore" process: use
unserialize()to convert$db_snapshotback into a PHP array. - Print the restored array to the screen using
print_r()to verify that the structure (including the nested array) remained intact.
There are no comments for now.