Reading a signed 64 bit integer from a file with PHP

Reading a signed 64 bit integer from a file with PHP

This may be a ridiculous way to do this but I couldn’t think of anything better at the time of writing. The function takes in $db which is the handle to a file pointer resource (e.g. the return of an fopen() call) and returns the result as an int, or float if it overflows.

I basically read 2x 4 byte binary strings from the file and unpack them as unsigned 32 bit (big endian)  longs. I then check if it’s negative by checking if the first bit of the first 4 byte string is a 1 (using binary AND). I strip the sign bit (with binary AND again) and add the two numbers together – first shifting the first 4 bytes I read 32 bits to the left. I then re-sign the number if it was originally negative by multiplying it by -1. Finally I return the result.

Do feel free to leave a comment if you know a better way to do this. I’m all ears!

  1. <?php
  2. function read64_int($db) {
  3. $first = reset(unpack('N', fread($db, 4)));
  4. $second = reset(unpack('N', fread($db, 4)));
  5.  
  6. // Get whether -ve (& 1000...)
  7. $negative = (bool)($first & pow(2, 31));
  8.  
  9. // Strip the sign (& 0111...)
  10. $first &= (pow(2, 31) - 1);
  11.  
  12. // Add the two
  13. $result = ($first << 32) + $second;
  14.  
  15. // Make it -ve if we need to
  16. if ($negative)
  17. $result *= -1;
  18.  
  19. // Return
  20. return $result;
  21. }
  22. ?>

When is downtime acceptable?

When is downtime acceptable?

I write this blog post as Twitter, a service used by millions of people is broken (again). Thankfully, this doesn’t bother me as Twitter MK1 (SMS) is still working – phew! The ‘fail whale’ is such a common occurrence that it has been satirised  in many forms and is seen as an every day occurrence. In my line of work, it is seen as detrimental to the existence of humanity to have 5 minutes of downtime on an e-commerce site (which barely pulls 2 transactions a week) or a small website (advertising the latest and greatest in replacement handles for your chocolate teapot).

Downtime is often unavoidable, especially if you rely on 3rd parties. Twitter has developed with downtime being a common occurrence and people seem content with this. Take an e-commerce site which costs £60,000 and has taken 6 to 8 months to develop. Is it acceptable that the previous version of the site is taken down for 30 minutes while the new one is put live? This seems to be frowned upon. Perhaps if the holding page had an amusing picture of a whale, this would be ok.

I don’t mean to sound resentful in my blog posts. There are lots of things I like. Such as hating every thing and everyone. I like that a lot. But seriously… if there’s one thing Twitter can teach us, it is that downtime is an inevitability and we should have a nice cup of tea, suck it up and stop whining.

Parsing a darkstat database with PHP

Parsing a darkstat database with PHP

This is something I wrote for the sake of interest more than necessity but I have found it quite useful for manipulating the data from a darkstat database since then. It’s not particularly elegant in such that it’s just a bunch of global functions but it’s a decent start if you want to wrap it in a class.

You can get the code here.

Randomising the order of posts in WordPress

Randomising the order of posts in WordPress

Just before ‘the loop‘ of a WordPress site, you would normally use query_posts(). Not only does this set a global variable somewhere deep inside the WordPress core to an array of posts but it also returns a reference to that array. This is pretty useful if you want to manipulate it before ‘the loop’. A good example of this is randomising the array, like I have with the last 3 posts on the home page of this site.

You can do this by simply wrapping the query_posts() call in a shuffle:

  1. <?php
  2. shuffle(query_posts()); // query_posts() will probably have some parameters passed to it. That's ok.
  3. ?>
Or you can assign the return of query_posts() by reference and shuffle that:
  1. <?php
  2. $posts =& query_posts(); // query_posts() will probably have some parameters passed to it. That's ok.
  3. shuffle($posts);
  4. ?>

 

Write a script or do it manually..? That is the question.

XKCD - The general problem

"I find that when someone's taking time to do something right in the present, they're a perfectionist with no ability to prioritize, whereas when someone took time to do something right in the past, they're a master artisan of great foresight."

The above cartoon sums up, in an amusingly satirical way, a major day to day conundrum of a developer. Do I manually process this workload or do I write some code to do it for me? Personally, I can’t stand the thought of doing the same thing over and over again and would rather take twice as long to write some code to do it than suffer the anguish of repetition.

This is more mild OCD than it is a wise business decision or good time management. If you multiply the time it takes to write the code by the number of times you’re going to do the same task at some point in your life, the conclusion may be different. However… you seldom know how many times you’re going to have to do something and, as such, the decision is a tough one.