10 years ago, in a Harvard dorm room, Facebook was launched. Initially, it was just Mark Zuckerberg hacking away at a codebase which consisted exclusively of PHP code, interacting with a MySQL database.

Since then, it has ballooned in size, both in terms of users and developers working on a codebase which is constantly growing. It soon became apparent that whilst PHP was a great language to start Facebook with, it was no longer suited the needs of the company.

And thus they created Hack, which is a purpose built language allowing for faster development, larger development teams, whilst maintaining full interoperability with the popular PHP programming language.

The reception of Hack by the development community has been nothing short of stunning. People are hugely excited about a language that is fast, yet easy to develop whilst maintaining backwards compatibility with the many PHP libraries in existence right now.

Here’s everything you need to know about installing Hack, as well as how to get your feet wet with the language.

What’s So Special About Hack?

Great question. You probably know that some programming languages are compiled to byte code which run on a special virtual machine (like Java and Clojure), whereas other languages (like PHP, Ruby and Python) run in an interpreter.

You also probably know that interpreted languages are inherently slower than languages which produce byte code which is specifically optimized for the virtual machines they run on. It's for this reason why there are dialects of Python and Ruby (called Jython and JRuby respectively) which run on the Java Virtual Machine.

Hack uniquely runs on its own virtual machine, called the Hip Hop Virtual Machine. It is this VM which has been used by Facebook to scale for billions of daily users.

Hack is more than the VM upon which it runs upon. It also comes with type annotations, allowing you to declare variables based upon the content which they will store (string, integer, boolean), lambda (anonymous) functions as well as generics.

Installing Hack

I’ve got some bad news for OS X and Windows users. Hack either doesn’t work on these platforms, or support is so flaky it isn’t worth discussing. Instead, you’re either going to have to fire up a Linux VPS or VM.

hack-ssh

Out of sheer laziness, I ended up settling on creating a Linux VPS with Digital Ocean, who are one of my favorite VPS providers. I created a small droplet running Ubuntu 13:10, and then installed Hack with the following commands.

        wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add -
echo deb http://dl.hhvm.com/ubuntu saucy main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install hhvm-nightly

As you can see here, I download the GPG key for the Hack repositories; add the repository to my sources list; update my sources definitions; upgrade my system and then install Hack. Simple, really.

Hacking With Hack

Now, it’s time to write some Hack code. We'll start off with the tried and tested 'Hello World' program. This one will not run in a web browser, but rather print ‘Hello World’ to the console.

Create a new file called HelloWorld.php (yes, for some reason Hack insists on using .php as its default file extension) in your favorite text editor (my preferred choice is VIM) and add the following lines.

hack-hello
        <?hh
 echo "Hello World";

Which should print out Hello World to the console when ran with 'hhvm', like so.

hack-hello-run

Alright, let’s try and run a Hack program in the browser. First, we’re going to need to install Apache and PHP. I’ve touched on this in a previous article, but to refresh your memory, I’ll run through it again. Run the following commands.

        sudo apt-get install apache2
sudo apt-get install php5

You can also install Apache, PHP, MySQL and a whole bunch of other useful utilities with the following command. (Don't forget the caret at the end of the line - lamp-server is not a single package, but rather a collection)

        sudo apt-get install lamp-server^
    

You are recommended to run this if you plan to take a closer, more serious look at web development with the Hack programming language, as it contains a lot of tools which you might find quite useful.

Once you’ve done that, restart the Hip Hop VM with the following command.

        sudo /etc/init.d/hhvm start
    

Check that Apache is running by opening a browser and navigating to the IP address of your web server. If everything is running, you should see this.

hack-itworks

Great! Now, navigate to /var/www and remove the page you just saw (called ‘index.html’) with the following commands.

        cd /var/www
rm index.html

Now, create a file called index.php and add the same lines you wrote before.

        <?hh
 echo "<p>Hello World</p>";

Once you've done that, revisit your web server with your chosen web browser.

hack-helloworld-browser

As you can see, there’s some weirdness with Hack recognizing the end of a string. As a result, I’d like to take this opportunity to remind you that whilst this technology is quite cool, it’s still pretty raw, and possibly not ready for production deployments. However, I will add that I am running the nightly version of Hack, which is the most bleeding edge version available. As a result, it shouldn't be too surprising that some things don't run perfectly well.

If you know PHP, you might recognize the 'Echo' statement I used before. Well, Hack can call any PHP function. Here I am calling 'phpinfo();' within a Hack program.

hack-phpinfo

Some Hack Specific Functionality

Hack brings a whole lot to the table, with respect to new language features. I discussed some of them before, including type declarations. Sadly, we're not going to be able to cover everything in this one article, but I figured it might be a good idea to look at how Hack handles type definitions.

So, to recap, type definitions are where you define a variable based upon the contents it would hold. How does that work? Well, a bit like this.

hack-types
        
<?hh
 bool $trueorfalse = false;
 string $myname = 'Matthew Hughes';
 int $myage = 22;

As you can see, we start off with the type declaration, followed by the name of the variable (starting with a dollar sign, much like in traditional PHP), followed by the value assigned to the variable.

This also comes into play with function declarations. When you’re declaring a function and passing it a parameter, you have to declare the type of variable you’ll be passing into it. If you don’t, or pass in the wrong type of variable, expect wailing and gnashing of teeth.

So, how does this actually work in Hack? Let's find out.

Create a new file called 'function.php' and write the following lines.

hack-function
        
<?hh
function hello (string $yourname): void {
 echo 'Hello ' . $yourname;
}
hello('dave');

If you've used PHP in the past, you might be able to decipher some of this. We’ve created a function, which we've called 'hello'. We then pass it a string, which is then echoed to the console, following the word 'Hello'.

But what's that : void bit? Well, in Hack, we have to tell the computer whether the function will be returning a value. If it isn’t, we say that the function is 'void'. As everything with Hack, we have to declare the type of values that will be returned.

We then run this code with the 'hhvm' command line application, and we should see this.

hack-hhvm-hello-dave

Conclusion

There’s a lot to love about Hack.

It’s a language which mandates you to write better code, whilst being easy to understand and ridiculously fast. It also has the support of one of the largest technology companies around right now, who use it daily in production to make one of the largest sites in existence scale to millions of users.

Despite that, it’s still a very new language. In fact, I wrote this article the day after it was released to the public. It's hard to recommend that anyone starts putting it into production right now, but certainly play around with it.

But that’s just my opinion. Will you be giving it a try? Drop me a comment below and let me know what you think.