Pages

Tuesday, March 25, 2014

What is Hack?

Hack is a programming language for HHVM that interoperates seamlessly with PHP. In general, anything you can write in PHP, you can also write in Hack. Take the example in the What is PHP section.
Example #1 An introductory Hack example
<?hhecho "Hi, I'm a Hack script!";
The primary changes were changing the <?php at the top to <?hh and removing the HTML (and the change to the output string, obviously).
Note:
Hack and HTML code do not mix. Normally you can intersperse PHP and HTML code together in the same file like:
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <!-- hh and html do not mix -->
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>
This code works fine in HHVM. You may jump in and out of Hack mode in an HTML file like this anywhere you want. But if you replace the <?php with <?hh, the code above will not work.
Of course, Hack can support more complex scenarios, but starting with Hack can really be as simple as adding <?hh to the top of your existing PHP file and writing the code to which you are accustomed, incrementally using Hack features at your leisure.
Note:
It is important to note that HHVM can run both your PHP and Hack code, either separately or when both are part of one project. HHVM is a runtime for both PHP and Hack.

No comments:

Post a Comment