ArticleslgStudy

computer science

Hack (programming language)

Hack (programming language) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Hack (programming language) rather than just read about it. In short: Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Meta (as Facebook) as a dialect of PHP. The language implementation is free and open-source software, licensed under an MIT License.

Hack (programming language) — main illustration
Hack (programming language) — illustration

Key takeaways

  • Hack (programming language) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Hack (programming language) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Hack (programming language) from memory before moving on to harder problems.

Reference excerpt

Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Meta (as Facebook) as a dialect of PHP. The language implementation is free and open-source software, licensed under an MIT License. Hack allows use of both dynamic typing and static typing. This kind of a type system is called gradual typing, which is also implemented in other programming languages such as ActionScript. Hack's type system allows types to be specified for function arguments, function return values, and class properties; however, types of local variables are always inferred and cannot be specified.

History Hack was introduced on March 20, 2014. Before the announcement of the new language, Facebook had already implemented the code and tested it on a large part of its web site.

Features Hack is designed to interoperate seamlessly with PHP, which is a widely used open-source scripting language that has a focus on web development and can be embedded into HTML. A majority of valid PHP scripts are also valid in Hack; however, many less-often used PHP features and language constructs are unsupported in Hack. Hack extends the type hinting available in PHP 5 through the introduction of static typing, by adding new type hints (for example, for scalar types such as integer or string), as well as by extending the use of type hints (for example, for class properties or function return values). However, types of local variables cannot be specified. Since Hack uses a gradual typing system, in the default mode, type annotations are not mandatory even in places they cannot be inferred; the type system will assume the author is correct and admit the code. However, a "strict" mode is available which requires such annotations, and thus enforces fully sound code.

Syntax and semantics The basic file structure of a Hack script is similar to a PHP script with a few changes. A Hack file does not include the <?php opening markup tag and forbids using top-level declarations. Code must be placed in an entrypoint function. These are automatically executed if they are in the top-level file, but not if the file is included via include, require, or the autoloader. Like other functions in Hack, the function names must be unique within a project – i.e., projects with multiple entrypoints can not both be called main:

The above script, similar to PHP, will be executed and the following output is sent to the browser:

Unlike PHP, Hack and HTML code do not mix; either XHP or another template engine needs to be used.

Functions Like PHP 7, Hack allows types to be specified for function arguments and function return values. Functions in Hack are thus annotated with types like the following:

See also

Phalanger

References

External links Official website and language reference Facebook Q&A: Hack brings static typing to PHP world A tutorial on converting an existing PHP project to Hack KPHP: An Executable Formal Semantics for PHP

Worked examples

Example 1 — a first encounter with Hack (programming language)

Start with the simplest possible case. Write down what Hack (programming language) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Hack (programming language) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Hack (programming language) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Hack (programming language)

In research
Hack (programming language) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Hack (programming language) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Hack (programming language) is common in secondary-school and first-year university syllabi. It links to neighbouring topics 2014 software, Dynamically typed programming languages, Facebook, so understanding it makes those chapters shorter.
In everyday life
Look for Hack (programming language) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Hack (programming language)” →

How to study Hack (programming language) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Hack (programming language) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Hack (programming language) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Hack (programming language) in simple terms?

Hack is a programming language for the HipHop Virtual Machine (HHVM), created by Meta (as Facebook) as a dialect of PHP. The language implementation is free and open-source software, licensed under an MIT License.

Why does Hack (programming language) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Hack (programming language)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Hack (programming language).

Tags

  • 2014 software
  • Dynamically typed programming languages
  • Facebook
  • Programming languages compiled to bytecode
  • Programming languages created in 2014
  • Scripting languages
  • Software programmed in PHP
  • Statically typed programming languages

Keep exploring