Disclaimer: I hate with a passion, inefficient code. I hate with a passion, unreadable code. These two factors cause this post to be strongly verbose. If you don’t like it, don’t read it.

To start this off, I want to say that PHP is one of the easiest programming languages out there that actually has a purpose. Visual Basic and C# are also both very easy, but unfortunately, they use the .SUCK framework, thus rapidly killing their usefulness. Any programmer who uses them for anything other than the simplest of tasks should be dragged into the street and beaten.

There are a number of very simple steps to follow when programming in PHP, and unfortunately, the majority of PHP sites whose code I see fail horribly to follow these. I’m not sure if the programmers were absent on “don’t suck at life” day in PHP class, or if they just don’t understand the concepts of “a processor has to parse and execute this”, “a network connection has to carry what this outputs”, and “other programmers will probably have to read this in the future”.

First and foremost, your code should be efficient. Most people will tell you that the golden rule is readability; well, that’s only true to an extent. Your code should be tabbed. However, your tabs should be tabs and not a bunch of spaces – four spaces takes up 4x the space that one tab takes. Double quotes (“) should NEVER EVER EVER be used. I have linked a benchmark of the various output methods in PHP (scroll down). I didn’t conduct this benchmark, so credit goes to “phpnet@i3x171um.com”.

There is no reason to use large APIs for things such as database abstraction (ADODB on PHP for instance). Code does not need to be portable when writing a custom app in PHP; it’s more than likely only ever going to be run on one server, and even if it’s run on other servers, the new one will more than likely be configured to act like the old one anyway. A good server administrator will ensure this. I digress; code does not need to be portable in PHP, and code like my example has to be parsed at every page request. It doesn’t even make your code much more portable, you’re still submitting raw queries; if you’re using MySQL, you’re still giving it MySQL code to execute, which isn’t going to work on every database engine anyway. And it doesn’t make the programmer’s life any easier, because the programmer still has to do error handling for every query, and ADODB is nothing but an added pain in the ass for the server and future programmers.

This is for both readability and efficiency. Your code should be tabbed, and your tabs should be actual tab characters, not a series of spaces. If you can’t tell what yours are, turn on your whitespace characters. If you can’t, get a different editor, AEdiX is great. If you use Linux and don’t feel like installing and configuring WINE to run AEdiX (a Windoze editor), BlueFish is also great. If your tabs are spaces, people who view whitespace have to sit there and look at a bunch of dots all over the screen, and the server has to look at (assuming a tab width of 4) 4 whitespace characters for every one it should have to look at. Oh yeah, and if your editor has “smart tabs”, do us all a favor and don’t use them. Every editor is going to deal with them differently, and only your editor is going to display them correctly. One minor thing that falls into tabs and whitespace, is do not leave any trailing whitespace at the end of your lines of code. It makes you look like a retard, and it makes the server work harder.

The server should never have to do constant math. For instance, if you need the number of seconds in a day, you should not be telling the server to use “24 * 60 * 60″. You should be telling the server to use 86400. If you don’t know the solution to your equation offhand (although as a programmer, especially a web programmer, you damn well should know that there are 86400 seconds in a day) and are incapable of a simple logic thread of basic math in your head, for the love of fuck, get a calculator. If you give the server “24 * 60 * 60″, it has to parse more than just the single number of 86400, it instead has to parse three numbers and two operators. It then has to execute these math operations, and if your server is running an x86-based platform, that’s another 2 CPU cycles per operation. Not to mention the added memory wasted by storing multiple values rather than just one. Oh yeah, plus the overhead of storage space on the server’s hard drives. Yes, servers have limited space, just like you and everyone else (moreso, in fact, because the 15000RPM+ hard drives that a server should be running are far smaller than the 5400RPM, 7200RPM, and maybe 10000RPM drive(s) that you have). For the same reasons (parsing overhead and storage requirements), the print operator should never be used; use echo instead.

Regarding MySQL queries. I don’t know if a lot of guys get wood for putting newlines in strings or what, but there is aboslutely no fucking reason that a MySQL query should look like this:

select address_id, addr1, addr2, city, state, zip1, zip2, assign_date, result_due_date, field_agent, agent_date,
contact_name, contact_phone, contact_email, contact_notes
from address
where status = 1

Absolutely no reason. I don’t know if it’s because PHP is the first language they’ve used that allows them to put newlines in strings and they’re exploring their new unbounded fucking freedom or what, but it’s entirely pointless, and places needless extra characters (newlines and tabs) inside the string, and the MySQL server has to parse these. It wastes memory by placing them in the string, wastes bandwidth between the PHP box and the MySQL box by having to send the extra characters, and then wastes memory on the MySQL box.

Additionally, still using the query from above, all MySQL keywords (in the case of the above example, “select”, “from”, and “where”) should be capitalized (SELECT, FROM, and WHERE). All identifiers (table names, database names, and row names) should be wrapped in backticks (ex. `address_id`, `address`, `status`), and the WHERE clause of the query should be wrapped in parentheses. Here is what that fucking monstrosity that some man calls a query should look like:

SELECT `address_id`, `addr1`, `addr2`, `city`, `state`, `zip1`, `zip2`, `assign_date`, `result_due_date`, `field_agent`, `agent_date`, `contact_name`, `contact_phone`, `contact_email`, `contact_notes` FROM `address` WHERE (`status` = 1)

(Your browser probably caused made that auto-wrap at the first and maybe second line, and will probably make the second one wrap twice, maybe three or four times…take my word for it that there are no newlines in that one.)

That’s all for now.