Password Protect a Directory with .htaccess
15 March 2005 | 98 comments | howto, webdev, security, programming, php, password, protect, sysadmin, htaccess, website, apache, directory, generator
A tutorial explaining how to retrict access to a directory on a web server using .htaccess.
A bookmarklet that allows you to have a number placed beside each of the results of a Google search.
Preload Images with CSS
23 December 2004 | 58 comments | code, rollover, webdev, howto, preload, web, design, image, webdesign, images, css
How to preload images using CSS and so avoid delays with rollover effects.
View Page Structure
12 October 2004 | 26 comments | imported, code, xhtml, tool, cheatsheet, design, webdesign, useful, tools, resources, css
A tool that outputs the structure of a page. Makes working with CSS (especially resolving inheritance issues) much easier.
Flesch-Kincaid Reading Level
07 July 2004 | 14 comments | reading, webdev, text, programming, reference, php, writing, readability, language, accessibility, algorithm
Functions to count the number of syllables in a word or sentence, and work out the readability of text.
The Gunning-Fog Index is a measure of text readability based upon sentence length and difficult words in a passage.
A tool to help automatically fix most common (X)HTML errors before outputting a page to the user.
28k and 56k Modem Emulator
29 June 2004 | 65 comments | webdev, development, web, design, usability, modem, webdesign, tools, accessibility, optimization, testing
Regardless of your connection speed, this will show you how your site loads on PCs with older connections.
Faux Columns for Liquid Layouts
22 June 2004 | 30 comments | tutorial, html, fluid, layout, reference, liquid, design, web, faux, webdesign, css, hacks
Using CSS for layouts can be a problem when using backgrounds for two columns that are not of equal length. This article expands on the solution to this problem from AListApart.
Ternary Conditionals
02 June 2004 | 28 comments | webdev, development, tutorial, programming, ternary, tips, php, coding
Ternary conditionals (using the "ternary operator", sometimes known as the "trinary operator") are a part of PHP that many simply steer clear of, despite their usefulness. They can save a great deal of time when writing code and can make for much easier code to read and edit later on. They look strange to many people though, which might explain why they are not as widely used as they could be.
Consider a normal conditional statement, like the following. It begins by evaluating a condition. If that condition is true, it follows one path. Sometimes, an alternate path is specified if the condition is not true (the 'else' section). Sometimes, you can have a list of several possible conditions in a row (using 'if ... elseif ... else' or 'switch ... case').
if (condition) {
variable = value-if-true;
} else {
variable = value-if-false;
}
However, a simple situation like the above is a perfect candidate to convert to a ternary conditional. You have one condition, and if it is true, the variable is given a certain value - if false, a different value. A ternary conditional can accomplish the same thing, concatenating it into one simple line of code.
variable = (condition) ? value-if-true : value-if-false;
Ternary conditionals take the above form. You do not necessarily need to have a "variable = " section (as you will see later on), but usually that is what this is used for. The above does exactly the same thing as the 'if ... else' statement earlier. If the condition evaluates to true, the variable will be assigned the value in the "value-if-true" section, otherwise it will receive the "value-if-false" value.
In practice, you could use the ternary conditional to, for example, greet a user depending on whether it is currently morning or afternoon. Using traditional code ('if ... else'), you might write something like this:
if (date("G") < 12) {
echo 'Good morning';
} else {
echo 'Good afternoon';
}
The same statement, using a ternary conditional, would look like this:
echo (date("G") < 12) ? 'Good morning' : 'Good afternoon';
Note that in this example, we've used "echo", rather than assigning a value to a variable. The above is exactly the same as this, which does make use of a variable:
$greeting = (date("G") < 12) ? 'Good morning' : 'Good afternoon';
echo $greeting;
Another situation in which I often use ternary conditionals is when displaying rows of data. It can often be much easier for a user to see what is going in if the rows alternate background colour, and the following code can be useful for that:
$i = 1;
echo '<table>';
while ($data = mysql_fetch_array($result)) {
echo ' <tr>';
echo ' <td bgcolor="';
echo (($i % 2) == 0) ? '#eee' : '#ddd' ;
echo '">';
echo $data['field'];
echo ' </td>';
echo ' </tr>';
$i++;
}
echo '</table>';
The above code will cycle through a result set, displaying each item in a new row. The background colour of the row will alternate between shades of grey, controlled by the ternary conditional on the bold line.
Ternary conditionals make for tidier code. Use them - if not for yourself, then for whoever is going to end up editing your scripts!