Page 32 of 33

Accounting for Computer Scientists

I’ve been reading (the very rare) posts by a “software craftsman” (as he calls himself) named Martin Kleppmann for a while now, and yesterday he wrote an ingenius post about accounting. If you’re in the same position I was in a couple years ago – I was a freshly baked computer scientist who just started a company and all the accounting jargon left me thoroughly confused – you might find this really invaluable.

So, head over to his blog and read his guide to accounting for computer scientists. It’s brilliant. 🙂

Convert large numbers into a more readable format

It can be tricky to really understand the value of 7312946 or 983217 at the first glance. Saying 7.3M and 983k seems like a much better option. How to convert numbers into a nicer format? Like this:

/**
 * create a nicely formatted number
 * @param $number a large number
 * @param $shorten default true = append k or M, false = format with commas and dots
 * @return int
 */
function createHumanReadableInteger($number, $shorten = true) {
	if (is_numeric($number)) {
		if ($shorten) {
			if ($number < 100000) {
				$return_int = number_format($number, 0);
			} else {
				$number_proc = $number;
				$units = explode(" ", "B k M G T P");
				for ($i = 0; $number_proc > 1000; $i++) {
					$number_proc = $number_proc / 1000;
				}
				if ($number > 1000000) {
					$return_int = round($number_proc, 2) . " " . $units[$i];
				} else {
					$return_int = round($number_proc, 1) . " " . $units[$i];	
				}
			}
		} else {
			$return_int = number_format($number, 0);
		}
		return $return_int;	
	} else {
		return 0;
	}
}

Avoid broken links on your website

We’ve all been there – finding a link on our website that’s been broken for a couple of months and that has probably annoyed hundreds (if not thousands) of our visitors. Little annoyances like this can be enough for our visitors to get a feeling that we’re running a low quality website. What’s more, search engine spiders won’t love you for broken links either – you might get a small penalty for having broken links on your website, and you might be pushed down a bit in the search engine results.

Because of this it’s extremely important to make sure (every once in a while) that all the out-going links on your website actually work. I suggest you google for “xenu broken link finder” – the website looks a bit shady, but the software is really good and will check all your backlinks for you. It will actually check all links, images (including background images), frames, style sheets (CSS files), javascript files and java applets.

What do your visitors really want?

This is most certainly one of the biggest questions in the online business. You don’t usually come in contact with your visitors as much as you’d want, and even if you do, you don’t always ask the right questions and get the best answers. Because of all this, it can be extremely helpful to use some tools that enable you to discover some of the background of your visitors.

With Google Trends for Websites you can analyze any website’s visitors, be it your own website or your competitor’s website. You can figure out what country / region represents the largest portion of website’s visitors, what other websites those visitors like as well and what those visitors have been searching for in the past. This way you can figure out what your visitors are interested in and you can make sure that your website gives them exactly what they want.

Because of this, they’ll love you and come back for more.

Converting a DATETIME variable into a UNIX timestamp

Here’s how:

/**
 * convert a datetime value (Y-m-d H:i:s) to UNIX timestamp value
 * @param datetime $datetime
 * @return timestamp
 */
function datetime_to_timestamp($datetime) {
    list($date, $time) = explode(" ", $datetime);
    list($year, $month, $day) = explode("-", $date);
    list($hour, $minute, $second) = explode(":", $time);
    return mktime($hour, $minute, $second, $month, $day, $year);
}

Using subkey to sort an array in PHP

It’s a bit tricky to sort multidimensional arrays in PHP by one of array’s subkeys, but it can absolutely be done. Check it out:

/**
 * USING POINTER TO ARRAY - sort an array of arrays with associative indeces by one of those associative values
 * @param $array pointer to our array
 * @param $subkey associative index that we want to order by
 * @param $sort_type [SORT_ASC|SORT_DESC]
 * @return void
 */
function sortBySubkey(&$array, $subkey, $sort_type = SORT_ASC) {
   foreach ($array as $subarray) {
      $keys[] = $subarray[$subkey];
   }
   array_multisort($keys, $sort_type, $array);
}

$1,000,000 a year – how difficult is that, really?

If you haven’t heard of David Heinemeier Hansson yet, you should check out the presentation he gave at Startup School ’08. It’s an amazingly motivational video and it got me thinking …

If you want to earn $1,000,000 a year, you only need 2,000 subscribers for your $40-a-month service. How difficult is it really to get 2,000 users for your high quality online service? Well, 2,000 people is .00011765% of people who use the internet. In other words, you need to have one paying subscriber in every city with 850,000 residents. Seems doable? 🙂

Creating absolutely safe strings

Sometimes you are given a string (someone’s name, or some title, or something completely different) and you cannot afford to use this string if you’re not completely sure what to expect. For instance, sometimes you need to convert a string into something that consists only of alphanumerical characters and spaces. You can always use the following function:

/**
 * create a string that only consists of alphanumerical characters and spaces
 * @param $string
 * @return string
 */
function createSafeString($string) {
	$string = strtolower($string);
        $output = "";
	for ($i = 0; $i < strlen($string); $i++) {
		$ord = ord($string[$i]);
		if (($ord >= 48 && $ord <= 57) || ($ord >= 65 && $ord <= 90) || ($ord >= 97 && $ord <= 122) || ($ord == 32)) {
			$output .= $string[$i];
		}
	}
	return $string;
}

Don’t manually escape all your variables

Let’s start with something fairly simple, shall we?

I’ve seen way too many programmers escaping all $_POST and $_GET variables like this:

$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

I suggest you simply use something like this from now on:

$post = array();
$get = array();
foreach ($_POST as $key => $value) {
	$post[$key] = escape_string($value);
}
foreach ($_GET as $key => $value) {
	$get[$key] = escape_string($value);
}
/**
 * escape given variable so we can use it in an SQL query
 * @param anything $value
 * @return anything $escaped_value
 */
function escape_string($value) {
	if (get_magic_quotes_gpc()) {
		$value = stripslashes($value);
	}	
	if (!is_numeric($value)) {
		$value = mysql_real_escape_string($value);
	}	
	return $value;
}

This way you can always use $post instead of $_POST and $get instead of $_GET if you need escaped values. And you can always access the original values in the original $_POST and $_GET variables. Pretty neat, huh? 🙂

Finding relevant keywords

It’s always more than just smart to find keywords with the biggest potential when you’re building a new online business. In short, it means that you should find keywords that accurately describe your business (your service or product), and then you should filter these keywords by the number of strong competitors and number of people who are searching for these keywords each month. You’re always looking for keywords with the lowest number of competitors and the highest number of monthly searches – and you always have to make a compromise.

But sometimes it’s difficult enough to come up with a list of potential keywords and key phrases. Many are using the AdWords External Keyword Tool. It is a great tool, but it’s not the only one out there. It’s not even the only one that was created by developers at Google. You should also check out Google Sets – it’s part of Google Labs and it will come up with a number of relevant and related keywords for your niche. Try it out. 🙂