Serve 304s from PHP: Conditional GETs for Dynamic Content
Browsers are begging to skip downloads they already have - your PHP just never answers. ETag and If-Modified-Since handling turns repeat views into empty 304 responses.
Software Engineer
I build web applications and share what I learn along the way.
Step-by-step programming tutorials on PHP, JavaScript, Laravel, WordPress and more.
Browsers are begging to skip downloads they already have - your PHP just never answers. ETag and If-Modified-Since handling turns repeat views into empty 304 responses.
The options-array setcookie signature made secure cookies writable in one call - and the __Host- name prefix gets the browser itself to enforce your security flags.
Prepared statements stop SQL injection - but % and _ sail straight through into LIKE patterns, letting users dump whole tables or run pathological scans. The fix is three characters of escaping.
Warnings scroll past in logs while your app continues in a corrupted state. One error handler converts them to catchable ErrorExceptions - and changes how you write file and network code.
No cURL, no Guzzle, no problem: stream contexts give file_get_contents methods, headers, bodies, timeouts - plus a magic variable that captures response headers.
Caching computed data per object with static arrays pins those objects in memory forever - a leak with a cache's haircut. PHP 8's WeakMap caches without owning.
When each match needs logic - lookups, formatting, escaping - preg_replace hits its ceiling. The callback variant runs code per match, and preg_quote keeps user input from breaking your patterns.
Mass-assignment bugs happen when request data flows unfiltered into updates. Two array functions - intersect_key and diff_key - are the terse, auditable filter.
"item10" before "item2", "Öl" after "z", uppercase clustering before lowercase - default sorts betray human expectations three different ways. PHP has a fix for each.
Keyed destructuring, foreach unpacking, swaps, nested extraction - the [$a, $b] syntax has quietly become one of PHP's best readability tools.
Every date in a month, every Monday for a year, billing cycles between two dates - DatePeriod iterates ranges natively, immutably and off-by-one-free.
Currency placement, month names, ordinal suffixes, "1.234,56" vs "1,234.56" - the intl extension knows every locale's rules, and most PHP developers never call it.