Cheat Sheets
Regular Expressions (V1)
CSS (V1)
Articles
Writing Secure PHP, Part 4
The fourth part of the Writing Secure PHP series, covering cross-site scripting, cross-site request forgery and character encoding security issues.
Ten Ways To Improve Your Website Conversion Rate
Why worry about getting twice as many people to visit your site, when it can be far easier to double the number of sales from the people already visiting? Here are 10 ways to improve your website conversion rate.
My Site's Dropped!
Why sites usually drop in the SERPs and what to do if it happens to you.
Writing Secure PHP, Part 3
The third part of the Writing Secure PHP series, covering weak passwords, clients and more advanced topics.
Writing Secure PHP, Part 1
Learn how to avoid some of the most common mistakes in PHP, and so make your sites more secure.
The Box Model For Beginners
An explanation of what the box model is and how it is treated by different user agents.
Blog and Lab
Basic Linux Command Line Tips
Learning your way around the command line is essential if you are developing with PHP (or Python, or a myriad of other languages most at home on Linux) and interacting with your server(s) with anything other than FTP and basic control panels. Here are some tips and tricks I've found useful.
6 Dropbox Tips for Developers
Dropbox is an excellent cross-platform freemium file synchronisation and online storage application. If that doesn't have you salivating already, it has a few more tricks up its sleeve.
Click here to read this post »
14 June 2010 | 33 comments | dropbox, development, hacks, tips
What Makes a Great Developer?
What makes a truly great developer? Some might say a positive attitude. Some might say a high-sugar, high-caffeine, high-bacon diet. Some might say an absence of sunlight and as many monitors as a desk can support. I say pessimism and laziness are high up the list.
Click here to read this post »
17 April 2008 | 68 comments | philosophy, programming, software, development, career, tips, job, developer, blog
PHP Querystring Functions
Adding and removing variables to and from URLs using PHP can be a relatively simple process admittedly, but I have a couple of functions I use often to make the process even less time-consuming.
Add Querystring Variable
A PHP function that will add the querystring variable $key with a value $value to $url. If $key is already specified within $url, it will replace it.
function add_querystring_var($url, $key, $value) {
$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
if (strpos($url, '?') === false) {
return ($url . '?' . $key . '=' . $value);
} else {
return ($url . '&' . $key . '=' . $value);
}
}
Remove Querystring Variable
A PHP function that will remove the variable $key and its value from the given $url.
function remove_querystring_var($url, $key) {
$url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
return ($url);
}
05 December 2006 | 49 comments | links, code, development, url, querystring, reference, php, functions, programming, tips, variable
Three Bloglines Tips
Use Bloglines? You're not alone. Have a huge list of feeds in your sidebar, and never read anything because it takes too long? Once again, you're very much not alone. Find keeping up with Bloglines is taking up all of your time? You're one of millions. However, all is not lost. A few simple tricks will make your subscriptions easier to manage and less of a burden.
Folders
Bloglines supports folders, into which you can delicately place your various feeds, grouping them together by topic. If when you look at Bloglines all you see is a massive, unmanagable list of feeds, folders should be your first stop. However, when you create your folders, put some thought into how you set them up - once they're set up, changing them later is going to take time, and most people don't want to have to go through the process again. Also, don't name them just "Topic" - name them "01. Topic" instead - give each folder a number at the start and you'll be able to order your folders any way you like, ensuring similar topics are always grouped.
Folders also allow you to spend less time on Bloglines. Keep your personal feeds (yes, including comics) separate from your work ones. You can view the work ones at the office, without being distracted by Dilbert, and keep up to date with friends and pictures of cats when you've got the time. Keep your important folders at the top of your folders list and the task becomes even easier, as you'll avoid having a tempting folder in the middle of the more serious work-related ones.
Re-Name Feeds
Website feeds are listed automatically using the name given by the website owner for the feed. For some this may be a problem. For example, I am subscribed to a feed from an Apache blog, and the default name for this is simply "feather". While that might be what their blog is called, it's no use to me - a few months after I've subscribed, I'm going to have no idea what "feather" actually is, and will end up ignoring it. However, I've renamed it "Apache Feather Blog" - far more helpful to me.
Only Show Updated Feeds
Once you've got all of your feeds organised, you may find that Bloglines loads rather slowly. When you've got several hundred subscriptions, that can be a real problem. Fortunately, Bloglines gives you the option to only show feeds with new items - updated feeds. You can enable this under Feed Options in your Bloglines account settings.
Ternary Conditionals
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!
02 June 2004 | 27 comments | webdev, development, tutorial, programming, ternary, tips, php, coding
