-
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
64: Practice Exercise: Building a File-Based Caching Layer
How do I handle the expiration logic so the data doesn't stay stale forever?
This is the core of any caching system: the TTL (Time To Live). If you just check if a file exists, you've built a permanent archive, not a cache. I usually handle this by comparing the current time against the file's last modification time using filemtime().
Here is a quick way to structure that check. I'm using a 3600-second (one hour) window here:
$cacheFile = 'cache/product_list.json';
$ttl = 3600;
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < $ttl)) {
// The file exists and it's fresh enough. Use it.
$data = file_get_contents($cacheFile);
} else {
// Data is missing or too old. Go fetch it from the slow API.
$data = fetchSlowProductData();
file_put_contents($cacheFile, $data);
}
One thing to keep in mind: filemtime() results are cached by PHP for performance. If you're doing extremely high-frequency updates to the same file, you might need to call clearstatcache(), but for a standard API cache, you probably won't need to worry about it.
What's the best way to store complex data like arrays without losing the structure?
Since file_put_contents expects a string, you can't just throw a PHP array at it. You have two real choices: serialize() or json_encode(). I almost always lean toward JSON. It's slightly slower than native serialization, but it's human-readable, which makes debugging your cache files a thousand times easier.
If you're caching a result set from a database or a REST API, do it like this:
// Saving the data
$productData = ['id' => 101, 'name' => 'Mechanical Keyboard', 'price' => 120];
file_put_contents($cacheFile, json_encode($productData));
// Retrieving the data
$raw = file_get_contents($cacheFile);
$productData = json_decode($raw, true); // 'true' converts it back to an associative array
Just be careful with the json_decode second parameter. If you forget true, you'll get a stdClass object back instead of an array, which might break the rest of your code if you're expecting array syntax.
How do I turn a dynamic query or URL into a valid filename?
You can't use a URL or a SQL query as a filename because they contain characters like /, ?, and & that the filesystem will hate. To solve this, I use a hashing algorithm. md5() is perfect for this; it's fast, and while it's not secure for passwords, it's more than enough for generating a unique cache key.
Imagine you're caching results for different category pages. You can't name a file /api/products?category=electronics. Instead, do this:
$apiUrl = "https://api.example.com/products?category=electronics&sort=price_asc";
$cacheKey = md5($apiUrl); // Returns something like '8f92b... '
$filename = "cache/" . $cacheKey . ".json";
if (file_exists($filename)) {
// Load cache...
}
By hashing the entire request string, you ensure that if the user changes the sort order or the category, they get a different cache file, preventing "leaked" data from one request showing up in another.
📋 Practical Task
Practical Exercise: Building a Cached External Weather API Wrapper
Your goal is to create a script that fetches weather data for a specific city from a mock API (or a real one if you have a key) and caches the result to avoid hitting the API on every page load.
- The Setup: Create a folder named
cache/in your project directory and ensure it is writable. - The Logic:
- Define a city name (e.g., "London") as a variable.
- Generate a unique filename for that city using
md5(). - Implement a TTL of 10 minutes (600 seconds).
- If the cache is valid, load the data from the file.
- If the cache is expired or missing, simulate an API call (using
file_get_contentson a URL or simply creating a fake array of weather data), then save that data as a JSON file.
- The Verification: Print a message to the screen stating whether the data was "Loaded from Cache" or "Fetched from API". Refresh the page a few times to confirm it only hits the "API" once every 10 minutes.
There are no comments for now.