Code Snippets

PHP Snippets

Short answers to problems PHP has had for years, written for the version you are actually running rather than the one in the old blog post.

Short PHP answers, each with the mistake that makes the common version fail.

Requests. Check the request method, not a field, because a browser sends nothing for an unticked checkbox. Redirect a page with three lines and three ways to get it wrong, including a 301 the browser caches.

Images. Convert WebP to JPG or PNG (paint onto white first or transparency turns black), convert PNG and JPG to WebP while keeping the original when the WebP is larger, and check every image on a page from the command line in a deploy check.

Parsing and templates. loadHTML garbles UTF-8 because it assumes Latin-1; the post tests four fixes. Render Twig outside a controller by injecting the Environment.

Search these 7 posts

Everything in PHP Snippets