Welcome to a new exploratory post here on gHacks! I actually work as a web designer specializing in standalone PHP and MySQL based sites, and I've always wanted to write a bit about coding, but it doesn't really fit my own blog and I just don't have time to start a whole new blog for it. Since gHacks is a tech blog we thought we'd try it out here with Martin and see how it goes. if you like the idea of similar posts here on gHacks please let us know in the comments (and also if you don't), we want to post stuff you guys are interested in, so feel free to boo it on out of here! So here goes nothing:
If you've been coding for a while you probably have some set ways in which you work. I recommend you also widen this method to naming variables, which when done consistently can be a huge help, especially when you are using some fancy MySQL along with your PHP code. If you are building a site with 15 MySQL tables it is helpful if you know all the column names because you always name them in the same way, but the same thing goes for simple variables, and even ids and classes for HTML elements.
A very common example can be variables which hold a user's data. In my case I always use the variable '$u' for holding the username, and the variable '$p' for holding the password. If you are grabbing a lot of data, like first name, last name, email and so on you can make up your own standard set, I use '$fn', '$ln' and '$e' respectively for the three.
Since you usually use these variables in the same script the importance of naming them consistently may not seem like a huge time saver. However when you get into session variables it saves you a lot of time if you don't always have to look at where you are defining them to find out the names of each. I pull user data using a mysql query when a user logs in and I immediately place the whole array in a session variable name "$_SESSION['user']". The names of the columns represent the values of this array, so the username and email would respectively become "$_SESSION['user']['Username']", "$_SESSION['user']['Email']". This is the same in my case for every single website I build where applicable.
User data naming consistency is also very prominent in the MySQL tables themselves. As you can see above my columns are also named the same in each case, a user's email is always "Email", his last name is always "LastName". I steer clear of abbreviations with MySQL tables so other coders can easily see what's going on as well. Consistency doesn't just apply to the actual names, you can also apply it to cases. As you can see all my column names are always capitalized, they are like wikiwords, but user session variables which don't come from MySQL are not capitalized. For example, when a user logs in he is assigned a time, which is usually one hour. If he/she is inactive for more than this time (no refreshes), he/she is logged out. There is no need to store this in the database, and so I know that this is not in there the session variable here is "$_SESSION['user']['time']". This means that if I come back to work on a site a year after completion I know exactly where variables come from.
Even if you don't yet know any PHP, you're just getting into HTML and CSS you can still use these good practices. When assigning ids and classes to elements try and work out a structure for yourself that you always use. I use empty div tags to clear floats a lot, so I have a separate class named "cl" for this purpose. Whenever I start out a new webpage I include this class by default in the stylesheet since I know I will most likely need it.
That's about it for standardizing your coding work, most of it is just common sense really. Try and work out your own method which works best for you. It takes time and a lot of practice to get a system working well since there is so much to think of, but if you always keep this in mind somewhere while coding you'll be a faster and better coder in no time at all!
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.