The 5 Best Things About Being A Developer

I was at work today and had a few of these happen. Because of that, I was inspired to write this post. These things happen to programmers young and old, novice or expert. Here’s the list.

1. Writing code that works the first time

Oh sure. All of us write flawless code, right? There’s just something that feels great about writing a sizeable portion of code and then having it do exactly what you wanted straight away. Especially if you’re newer with a certain language. Confidence++.

2. Finishing a Project

Starting a project is exciting. Finishing is ethereal. My typical work method is based on doing all of the hard work first, thereby eliminating any unintended delays before launch. Unfortunately this means that the last 10% of any project is the most tedious and dreary as it’s filled with things like pixel-hunting or resizing images. Once the last few lines of code are written and tested, life is bliss.

3. Optimization / Refactoring / Reducing # of Lines of Code

Few things in development are as good as the feeling you get when you took that 4x nested for-loop with its switches and if blocks and converted it into a simple, 5-line function. It’s why I get up in the morning. OK maybe not that exciting, but still.

Optimization may not be the best description for this, but in a way it is a sort of optimization of how efficiently your code works.

4. Seeing Marketing For a Product You Worked On

I smile every time I visit my parents as there is a very large billboard advertising a site that I had a part in developing. I know that nowhere on the sign it says my name and that no one even knows I worked on it, but inside I feel good that someone, somewhere is finding something I helped to create useful.

Another example of this would be seeing people play a videogame you created or hearing your song on the radio (if you were a musician).

And, finally:

5. Learning Something New and Useful

I try to make it a point that every day I learn something new and useful to make my work better, faster, and more efficient. I say “new and useful” since new isn’t always better than old. Especially with computers and software.

Whether it’s some new Linux commands (split and cat for me a few weeks ago) or a big jump like using source control, making yourself and your team better at what you do is what it’s all about.

PHP Tricks: How To Handle Multiple Domains

This is really handy for those of us who have the same code handling multiple sites or multiple sub-domains.

A case in point: When I coded NetBoardz (my free forum hosting service now defunct), I had one codebase handling all 250 forums. How? Simple. When the code runs, it determines which site the user is loading and does different things (like using different databases) dynamically.

How to determine the domain the user is using to view your site:

$domain = $_SERVER['HTTP_HOST'];
if ($domain == "xyz") {
    ...
} else if ($domain == "uvw") {
    ...
}

In the example above you can see that we have put the domain that the user has used to view your site into the $domain variable, loading the value from the PHP global variable, $_SERVER. The $_SERVER variable is global, which means you can access it anytime and anywhere in your code.

More information on PHP’s predefined global variables.

How to determine the sub-domain the user is using to view your site:

Sample code is from NetBoardz, which is based off of phpBB 2:

$subdomain = strpos($_SERVER['HTTP_HOST'], ".");
$subdomain = substr($_SERVER['HTTP_HOST'], 0, $subdomain);
$dbname = "nb_".$subdomain;
mysql_select_db($dbname, $sql_link);

Here you can see that we retreived the whole hostname, including the top-level domain and subdomain, then used the PHP functions strpos and substr to take anything before the first dot. For example, the whole hostname “testforum.netboardz.com” passed through this code would end up as “testforum”.

After, we use that subdomain name to calculate which forum database to load. Of course, once you have the domain or subdomain in a variable, you are able to handle your code as you wish!