PHP is far too cool
I’ve learned PHP mainly from looking at other people’s code and consulting books and the online manual when I didn’t understand something I saw. I’ve been using it for a bit more than two years now, but there are still large, sometimes very useful sections of the language which I’m still learning. This will probably be true for years to come.
Take, for example, getimagesize(). For years, I learned that pages render faster when the width and height of the image are specified in the code; that step allows the browser to allow space for the image immediately and lay out the page before the image is received from the server. If the size isn’t set, the image has to be downloaded for the browser to determine its size and lay out the page around it.
On the other side of the coin, PHP led me in to dynamic pages that had to include images, frequently without knowing the size. (For example, catalog detail pages on our company site will include a cover image only if the image exists; if not, the image tag isn’t written. The cover images vary slightly in size, so hard-coding the dimensions in the templates is out.) So in the name of flexibility, I left out the image size and simply included the reference.
With getimagesize(), I can check the size on the server side (conveniently skipping the “is it really there” step, which is included,) and write a complete image tag. I can even use fewer steps to check if the file is JPEG or PNG. Excellent.
(Look, most of the people I know read this now and then are probably much less impressed than I am. But here in the office, I’ll just get blank looks, and I’m excited about this. So nod politely, I’ll get back to less geeky stuff someday.)
For example, given the book keyed in our database as 0914, I can write this in the template (assuming $last4 is the database key, and $title and $author have already been determined from the database):
if ($img_size = (getimagesize("images/$last4.png")) || (($img_size = getimagesize("images/$last4.jpg"))) {
list($width, $height, $type, $attr) = $img_size // Get individual variables from the array
switch ($type) {
case 1:
$filestring = "images/" . $last4 . ".gif";
break;
case 2:
$filestring = "images/" . $last4 . ".jpg";
break;
case 3:
$filestring = "images/" . $last4 . ".png";
break;
}
echo "<img src=\"$filestring\" alt=\"$author: $title\" style=\"width: $width px; height: $height px; margin: 8px; float: left;\">\r";
}
and get this HTML out:
<img src="images/0914.jpg" alt="Coyne and Orr: Speciation" style="width: 125 px; height: 165 px; margin: 8px; float: left;">
That’s the XHTML; for HTML 4.x, I could drop in the $attr variable instead of the CSS size definition; it’s wasted in XHTML. I think. Technically, I could even drop in a Shockwave animation, but I don’t really think we’re there yet.
I realize this is probably neat like digital watches, but it’s already made my day.
Edit: Damn, I can tighten that up even more and save a variable…
Now playing: Within Your Reach from Hootenanny by The Replacements