Variables and Functions

Printing to the console is nice but can only get you so far. This week I will discuss the use of variables and functions in Hack. Now many of you who have been following along with my blog know that I have made a few comparisons from PHP to Hack in their similarities today we will explore one of thier differences.

Before we get into things I thought I’d show you how to setup the Hip Hop Virtual Machine (HHVM) as a local web server using the terminal.

HHVM activation
Activating HHVM as web server parameters: m ; mode set to server p the port to communicate 

Now that we got that out of the way let’s discuss variables in Hack.. A ‘$’ symbol must be used to define a variable. Such that $x = 5, would initialize the variable x with a value of 5. If we want to use we reference the variable x ,$ is just used to define the variable.

Don’t forget the semicolon !!!

Now that we understand how variables work in Hack let’s talk about functions. Unlike PHP, Hack functions can have specified return types. If these functions have parameters defined then you must define the datatype in the declaration of the function. Every function declaration starts with the keyword “function” followed by the name a list of parameters. If you want specify a return type for the function then type “:[datatype]”. This really handy if you have some complex object in which you wish to return.

Functions of Hack
This code features the declaration of 2 two functions, If you look closely this program will print the output of add_one(6);
Output
Here we have the output of the program on FireFox web browser, we added one to 6 and got 7

 

Hello Hack

Today I will show you how to execute the famous Hello World program using the Hack programming language.

Below is the code outputs that the string “Hello World!!”

 

Hello World
Hack code for Hello World Program

Now if any of you are familiar with the php language you will say that it looks just like it. You’d be almost correct the header <?hh makes this file a Hack file. One of the neat features of hack is that it can run php code without any modifications what so ever to it. By using <?hh the HHVM(Hip-Hop Virtual Machine) is called upon to execute the commnad(s). All hack files are saved using the .php format.  Next week I’ll explain more about the HHVM and how it makes Hack unique to PHP.

Hello World !
Hello World Program in FireFox Web Browser

Introductions

Hello my name is Niko, I will be demonstrating the Hack language to you.

Hack is a backend web language very similar to PHP. For those of you who are familiar with PHP you will know that Facebook uses it a lot. In fact Facebook developed Hack as an update to PHP. Backend web refers for operations for content retrieval. Unlike HTML which represents the front or surface of a webpage it is not very visual. Hack has some interesting features which include return types for functions,  TypeChecker, and HHVM. Throughout this blog I plan touch up on all these unique features.

If you are interested about learning the Hack language, comeback next week and visit hacklang.org for any additional information.

Next week I will show you just how similar Hack is to PHP with some simple code examples