Learn Hack - 10 Code Examples & CST Typing Practice Test
Hack is a programming language developed by Facebook as a dialect of PHP. It adds static typing, enhanced performance, and modern programming features while maintaining compatibility with PHP, enabling rapid development of large-scale web applications.
Learn HACK with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
Hack Counter and Theme Toggle
<?hh
<<__EntryPoint>>
function main(): void {
$counter = 0;
$isDark = false;
function updateUI(): void {
global $counter, $isDark;
echo "Counter: $counter\n";
echo "Theme: " . ($isDark ? "Dark" : "Light") . "\n";
}
function increment(): void {
global $counter;
$counter += 1;
updateUI();
}
function decrement(): void {
global $counter;
$counter -= 1;
updateUI();
}
function reset(): void {
global $counter;
$counter = 0;
updateUI();
}
function toggleTheme(): void {
global $isDark;
$isDark = !$isDark;
updateUI();
}
// Simulate actions
updateUI();
increment();
increment();
toggleTheme();
decrement();
reset();
}
Demonstrates a simple counter with theme toggling using Hack variables, functions, and conditional statements.
Hack Fibonacci Sequence
<?hh
function fib(int $n): int {
return $n < 2 ? $n : fib($n-1) + fib($n-2);
}
<<__EntryPoint>>
function main(): void {
for ($i = 0; $i < 10; $i++) {
echo fib($i) . "\n";
}
}
Generates first 10 Fibonacci numbers using recursion.
Hack Factorial Calculator
<?hh
function factorial(int $n): int {
return $n === 0 ? 1 : $n * factorial($n - 1);
}
<<__EntryPoint>>
function main(): void {
echo factorial(5) . "\n";
}
Calculates factorial of a number using recursion.
Hack Prime Checker
<?hh
function isPrime(int $n): bool {
if ($n < 2) return false;
for ($i = 2; $i < $n; $i++) {
if ($n % $i === 0) return false;
}
return true;
}
<<__EntryPoint>>
function main(): void {
echo isPrime(13) ? "Prime\n" : "Not Prime\n";
}
Checks if a number is prime.
Hack Sum of Array
<?hh
<<__EntryPoint>>
function main(): void {
$nums = vec[1,2,3,4,5];
$sum = array_sum($nums);
echo $sum . "\n";
}
Calculates sum of an array of numbers.
Hack Reverse String
<?hh
<<__EntryPoint>>
function main(): void {
$s = "HELLO";
echo strrev($s) . "\n";
}
Reverses a string.
Hack Multiplication Table
<?hh
<<__EntryPoint>>
function main(): void {
$n = 5;
for ($i = 1; $i <= 10; $i++) {
echo "$n x $i = " . ($n * $i) . "\n";
}
}
Prints multiplication table of a number.
Hack Celsius to Fahrenheit
<?hh
<<__EntryPoint>>
function main(): void {
$c = 25;
$f = ($c * 9 / 5) + 32;
echo $f . "\n";
}
Converts Celsius to Fahrenheit.
Hack Simple Alarm Simulation
<?hh
<<__EntryPoint>>
function main(): void {
$temp = 80;
$thresh = 75;
echo ($temp > $thresh ? "Alarm: Temperature Too High!\n" : "Temperature Normal\n");
}
Simulates an alarm if a threshold is exceeded.
Hack Random Walk Simulation
<?hh
<<__EntryPoint>>
function main(): void {
$steps = 10;
$pos = 0;
for ($i = 0; $i < $steps; $i++) {
$pos += (rand(0,1) === 0 ? -1 : 1);
echo $pos . "\n";
}
}
Simulates a 1D random walk.
Frequently Asked Questions about Hack
What is Hack?
Hack is a programming language developed by Facebook as a dialect of PHP. It adds static typing, enhanced performance, and modern programming features while maintaining compatibility with PHP, enabling rapid development of large-scale web applications.
What are the primary use cases for Hack?
Large-scale web application development. Maintaining and modernizing PHP codebases. Developing scalable backend services. Improving code safety with static typing. Rapid iteration and prototyping in web projects
What are the strengths of Hack?
Improves reliability with static typing. Maintains compatibility with existing PHP code. Enhances code readability and maintainability. Optimized for large-scale web applications. Supports modern programming patterns
What are the limitations of Hack?
Primarily used within Facebook or HHVM ecosystems. Smaller community compared to mainstream PHP. Requires HHVM runtime, not standard PHP engine. Not ideal for small-scale or non-web projects. Learning curve for developers new to static typing
How can I practice Hack typing speed?
CodeSpeedTest offers 10+ real Hack code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.