Examples

Runnable programs from the repo. Everything in the first table runs with hphp run examples/<name>.hphp; the self-tests live in tests/manual/ because they need a window, a port or threads.

⚡ Try these in the Playground 📚 Interactive Guide
FileWhat it shows
hello.hphpthe classic one-liner
demo.hphpthe language tour: functions, arrays, classes, closures, match, exceptions
ui_demo.hphpUI 2.0: themes, cards, forms, lists, meters, menus, timers
ui_gallery.hphpevery theme and every control, side by side
canvas_demo.hphpa raw canvas: draw calls and pointer/keyboard events as numbers
holyhtml_demo.hphpan HTML/CSS surface painted by HolyPHP, verified by reading pixels back
holyide/holyide.hphpthe IDE: explorer, tabs, editor, palette, terminal, runner
ws_chat.hphp + ws_chat.htmla browser chat server over RFC 6455
device_feed.hphpa WebSocket feed of smart-home JSON, one update per second
async_demo.hphptimers, TCP servers, HTTP in one event loop
thr_demo.hphpchannels, atomics, barriers, parallel map
websocket_demo.hphpthe WebSocket API without a browser
bench.hphp / record.hphpmicro-benchmarks, next to their hand-written C twins
tests/manual/*.hphpself-tests that open a window, bind a socket or spawn threads

hello.hphp — the classic

echo "Hello, HolyPHP!";

demo.hphp — the language tour

Functions, arrays, maps, classes, closures, match and exceptions in one file. Highlights:

function fib(int $n): int {
    if ($n < 2) { return $n; }
    return fib($n - 1) + fib($n - 2);
}
echo "fib(10) = " . fib(10);

$nums = [5, 3, 8, 1, 9];
$total = 0;
foreach ($nums as $x) { $total += $x; }
echo "sum = {$total}, count = " . count($nums);

$user = ["name" => "Ada", "lang" => "HolyPHP"];
foreach ($user as $k => $v) {
    echo "  {$k} => {$v}";
}

holyhtml_demo.hphp — HTML and CSS, painted by HolyPHP

One window, one canvas, and a document the engine parses, styles, lays out and paints. After a few frames the demo reads pixels back out of the surface and asserts that the sidebar is where the CSS says it is — a layout can be wrong while every state test passes; a pixel cannot.

import "html";
import "ui";

$W = 760; $H = 460;
$win = ui_window("HolyHTML", -1, -1, $W, $H);
$cv  = ui_add($win, 18, "", 0, 0, $W, $H);

$view = new HtmlView($cv, $W, $H, html_theme_dark());
$view->load("
EXPLORER
" . "
hello
", ".root { display: flex; height: 100%; }\n" . ".side { width: 200px; background-color: #252526; }\n" . ".main { flex: 1; background-color: #1e1e1e; }"); $view->paint(); ui_run_main();
hphp run examples/holyhtml_demo.hphp
# boxes=… sidebar: 0,0 200x460 … pixels ok

holyide/holyide.hphp — the IDE

The workbench from lib/ide in a window: explorer, tabs, a syntax-highlighted editor with undo and completions, a command palette, a terminal and a problems list. --selftest renders a few frames, reads the canvas and exits, so the wiring can be checked without a human.

hphp run holyide/holyide.hphp            # open the current folder
hphp run holyide/holyide.hphp ..         # another one
hphp build holyide/holyide.hphp -o holyide.exe
./holyide.exe --selftest                 # ok sidebar bg / status bg / title bg

async_demo.hphp — event loop, HTTP, threads

Timers with cancellation, a line-oriented TCP server with ACK replies, an in-loop HTTP GET, a blocking fetch on a worker thread, and raw watchers that see data-then-EOF:

import "async";

$loop = async_line_server("tcp://127.0.0.1:8931", function($c, $line) {
    return "ACK:" . strtoupper($line);
});

$loop->httpGet("http://127.0.0.1:8932/hello",
    function($resp) use ($loop) { echo strlen($resp) . " bytes"; $loop->stop(); },
    function($err)  use ($loop) { echo "failed: $err"; $loop->stop(); });

$loop->run();
hphp run examples/async_demo.hphp   # ASYNC DEMO: all 5/5 checks passed

thr_demo.hphp — real threads

import "thread";

$n = thr_parallel_map(function($x) { return $x * $x; },
                      [1,2,3,4,5,6,7,8,9,10]);
echo implode(" ", $n);         // 1 4 9 16 25 36 49 64 81 100
hphp run examples/thr_demo.hphp     # ALL THREAD TESTS PASSED
Tip: every example compiles to a standalone binary: hphp build examples/hello.hphp -o hello. The compiler embeds the runtime and the libraries, so the binary has no external dependencies.