PHP


I’ll try to keep this short. Having been coding for years I’ve seen a lot of examples of how other people write code. Now while there are certain requirements in coding, there are areas where you can inject your own person style or preference. One area that has always bugged me is how code is indented and how brackets are used to separate blocks of code. Here’s a very simple example of how I normally code a simple if/else block:

if (hours < 24 && minutes < 60 && seconds < 60)
{
 return true;
}
else
{
 return false;
}

This is known as Allman style (named after Eric Allman, the developer of sendmail). The most common variant of this would be K&R style (from Kernighan and Ritchie’s book The C Programming Language) and the same code looks like this:

if (hours < 24 && minutes < 60 && seconds < 60) {
 return true;
} else {
 return false;
}

Equivalent in every way except readability. Some people call this “The One True Brace Style” because it’s been around so long. The major difference is that the opening bracket is placed at the end of the line that the control statement is on where in Allman style the brackets are each on their own line.

To me it makes sense to make code as readable as possible by lining up brackets to match the block they go with. It becomes apparent what blocks of code belong to what conditions.

Allman style
 
K&R style

If I was to have another condition nested within the example, it would look like this:

if (hours < 24 && minutes < 60 && seconds < 60)
{
 if(hours % 2 == 0)
 {
 	return true;
 }
 else
 {
 	return false;
 }
}

Again, very readable.

The funny thing about K&R style is that its roots seems to be based in the fact that programmers used to have to deal with limited screen space and by squishing the brackets together with the blocks that they belonged to, it saved precious screen real estate. Most programmers either picked up the style they use most often from learning coding in a class or by following the examples set by others, while other programmers code for readability, especially now that space isn’t the issue it once was.

While Allman and K&R are just two of the most popular styles (see
http://en.wikipedia.org/wiki/Indent_style
and
http://en.wikipedia.org/wiki/Programming_style
for some others and a detailed explanation about the history of each)

(please pardon the less than perfect wordpress code formatting)

I wanted to make a small addition to the pagination class talked about at http://www.catchmyfame.com/2007/07/28/finally-the-simple-pagination-class/. I added another example which uses a basic form at http://www.catchmyfame.com/paginator/example-form.php. This is very similar to the first example (http://www.catchmyfame.com/paginator/example.php) where the query is hard coded. This new example is meant to show how the pagination class can be used with forms where the data determines the query. In this case, the pull down menu for a continent selection determines the query. Note that the pagination class works fine with both GET and POST requests.

In the years that I’ve been a web developer, I have written very few OOP PHP scripts. I just haven’t had the need to, and to be honest the whole OOP concept seemed like something I didn’t need to bother with. Why do I need to write a script in an object-oriented style and how will it help me? I had seen examples and read tutorials about OOP (some horribly bad and some really good) but never had any desire to try it myself until one day when I was working on a couple of different projects that both needed the same thing, pagination.

Pagination just means numbering pages. In the project that I was working on, I was retrieving large amounts of data from a MySQL database and realized that I would have to break the data up into pages for two main reasons, 1) there was so much data that it could hang the browser when it was loading and 2) from a user perspective (we still care about that right?) it was unruly to say the least. If I was returning 50,000 rows from a database query, I knew that I would need to slice that up into more manageable chunks. Now in the past, I had written basic pagination functions to handle this type of issue and sometimes copied and pasted functions from older scripts to cut down on development time, fixing and re-coding where necessary. But I realized that for this new project I could try to write a pagination class that I would be able to just plug into any script and have it do the pagination for me. I could write a pagination class that I could use for my projects and future projects, and learn OOP in the process. I love killing two birds with one stone.
(more…)

PHP, like most programming or scripting language allows for comparison operations bu using if/else. It’s a staple of any good language yet at the same time you can end up writing a lot of code in order to accomplish something very simple. Take for example this simple comparison:

if (empty($_POST['action']))
{
$action = ‘default’;
}
else
{
$action = $_POST['action'];
}

All we’re doing here is assigning one value to $action if the form variable action isn’t empty, and different variable if it is. This took up eight lines of code (OK, so you could argue that you could lower that by moving braces around but the code gets ugly fast). How can we clean this up? Enter ternary operators!
What’s a ternary operator you ask? In a nutshell, a ternary operator takes three elements and combines them into one. The basic syntax is (expression 1) ? (expression 2) : (expression 3); which means do expression 2 if expression 1 is true, otherwise do expression 3. It’s just an if/else condition rewritten in one simple line. Taking our previous example, we could consolidate it to this:

$action = (empty($_POST['action'])) ? ‘default’:$_POST['action'];

Voila! The exact same results in one easy to understand line. I am a huge fan of this shortcut and it comes in very handy when you have basic if/else conditions to check.