Once upon a time, when I was a few years younger, I was given a great little nugget of advice: be as lazy as possible
. It was actually my dad who gave me that advice and at the time I bet he wished he hadn’tMDASHnot the kind of advice to give a 14 year old guy. At the time it was music to my ears, but now I’ve realised what he meant…
When building websites there will always be repetitive and boring snippets of code that constantly need typing over and over and over and… anyway, you get the point. This was particularly noticable when making CSS Wizardry so I decided to build a way for abPHP to do it for me.
One thing I was contantly having to type was three consecutive html entities to create a hair spaced em dash. This code was:  — . As you can imagine it became tedious having to a) remember and b) type all that every time I wanted a simple em dash anywhere.
The solution was simple. The framework I use for CSS Wizardry has a config file, it contains image directories, abURLs and site data that gets written once and pulled in several times, wherever/whenever. I opened up the config file and simply appended the abPHP file with several constants containing boring snippets that I needed to use over and over:
<?php
//SET UP GENERAL SITE DETAILS
define('ROOT', 'http://csswizardry.com/');
define('SITE_NAME', $iniConfig['SiteData']['name']);
define('KEYWORDS', $iniConfig['SiteData']['keywords']);
define('DESCRIPTION', $iniConfig['SiteData']['description']);
[other constants]
//SET UP REPETITIVE CODE SNIPPETS
define('abCSS', '<abbr title="Cascading Style Sheet">CSS</abbr>');
define('abXHTML', '<abbr title="eXtensible HyperText Markup Language">xHTML</abbr>');
define('abPHP', '<abbr title="Hypertext Preprocessor">PHP</abbr>');
define('abIE', '<abbr title="Internet Explorer">IE</abbr>');
define('MDASH', ' — ');
?>
Pay attention to the bottom set of data. These short constants contain any mundane bits of code that I would normally write out into the views each and every time I needed it. However, by including the config.php at the top of each page like so:
<?php include("includes/config.php"); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
All I have to do is type <?php echo abCSS; ?> everywhere I would normally be typing <abbr title="Cascading Style Sheet">CSS</abbr> and the correct code is inserted for me:
<h2>Be as lazy as possible<?php echo MDASH; ?>On letting PHP do the mundane tasks</h2>
So, when doing repetitive tasks, don’t make extra work for yourself. Investing time in the beginning to plan ahead will save you much more time in the long run. Defining abPHP constants is just one such way of saving time and effort, and the best thing with this example in particular is that I can simply add more constants as they’re needed. It’s pretty scalable.