Compile PHP to
native binaries.
elephc is a PHP compiler that compiles PHP code directly to ARM64 assembly, producing standalone macOS executables. No interpreter. No VM. No runtime dependencies.
Compilation pipeline
How the PHP compiler
turns code into machine code
Six well-defined phases compile your PHP source into a standalone native binary. The compilation pipeline transforms PHP code into optimized ARM64 assembly, then assembles and links it into a Mach-O executable.
Lexer
Character-by-character scanning of PHP source into keywords, operators, literals, and identifiers.
Parser
Pratt parser with binding powers builds a structured tree of expressions and statements.
Resolver
Processes include, require, include_once, and require_once — merging multiple files into one AST.
Type Checker
Infers and validates types for every variable and expression. Catches errors before assembly is emitted.
Codegen
Every AST node is translated to annotated ARM64 instructions. Each line is commented explaining what and why.
Link
macOS as assembles to object code, then ld links into a standalone executable. Zero dependencies.
Language support
Supported PHP
language features
elephc compiles a growing subset of PHP to native code. Every program it compiles
is also valid PHP — producing identical output when run with php.
Six types are resolved statically at compile time.
Type system
int 42, -7, PHP_INT_MAX float 3.14, .5, 1e-5, INF string "hello\n", 'raw' bool true, false null null array [1,2], ["k"=>"v"] A variable's type is set at first assignment. Compatible types (int, float, bool, null) can be reassigned between each other. Type errors are caught at compile time with line:col error messages.
Supported constructs
Compile-time error messages
Code examples
Write PHP.
Compile to native.
25+ working PHP code examples ship with the compiler. Each one compiles to a standalone native binary and produces identical output to the PHP interpreter. From hello world to recursive algorithms — all compiled to ARM64 machine code.
All 25+ included examples
Standard library
100+ compiled PHP
built-in functions
The PHP compiler supports over 100 built-in PHP functions — from string manipulation and array operations to file I/O and math. Each function is compiled directly to native ARM64 instructions with its own documented codegen file.
Strings 55+ functions
strlen intval number_format substr strpos strrpos strstr str_replace str_ireplace substr_replace strtolower strtoupper ucfirst lcfirst ucwords trim ltrim rtrim str_repeat str_pad strrev str_split strcmp strcasecmp str_contains str_starts_with str_ends_with ord chr explode implode addslashes stripslashes nl2br wordwrap bin2hex hex2bin sprintf printf sscanf md5 sha1 hash htmlspecialchars htmlentities html_entity_decode urlencode urldecode rawurlencode rawurldecode base64_encode base64_decode ctype_alpha ctype_digit ctype_alnum ctype_space Arrays 35+ functions
count array_push array_pop in_array array_keys array_values sort rsort isset array_key_exists array_search array_merge array_slice array_splice array_combine array_flip array_reverse array_unique array_sum array_product array_chunk array_column array_pad array_fill array_fill_keys array_diff array_intersect array_diff_key array_intersect_key array_unshift array_shift asort arsort ksort krsort natsort natcasesort shuffle array_rand range Math 14 functions
abs floor ceil round sqrt pow min max intdiv fmod fdiv rand mt_rand random_int Types 15 functions
gettype settype empty unset is_int is_float is_string is_bool is_null is_numeric is_nan is_finite is_infinite boolval floatval I/O & Filesystem 30+ functions
fopen fclose fread fwrite fgets feof readline fseek ftell rewind file_get_contents file_put_contents file fgetcsv fputcsv file_exists is_file is_dir is_readable is_writable filesize filemtime copy rename unlink mkdir rmdir scandir glob getcwd chdir tempnam sys_get_temp_dir var_dump print_r exit die INF NAN PHP_INT_MAX PHP_INT_MIN PHP_FLOAT_MAX M_PI STDIN STDOUT STDERR Documentation
Learn how a
PHP compiler works
12 guides covering every aspect of building a PHP compiler — from lexing and parsing, to type checking, code generation, and the ARM64 instruction set. No prior assembly or compiler knowledge required.
What is a compiler?
The big picture: source code in, binary out
How elephc works
The full pipeline walkthrough, step by step
The Lexer
How source text becomes a stream of tokens
The Parser
How tokens become an AST with Pratt parsing
The Type Checker
Static types, inference, and error detection
The Codegen
How the AST becomes ARM64 assembly
The Runtime
Runtime routines: itoa, concat, hash tables, I/O
Memory Model
Stack, heap, concat buffer, hash tables
ARM64 Assembly
ARM64 primer for people who've never seen assembly
ARM64 Instructions
Quick reference for every instruction elephc uses
Language Reference
Complete spec: types, operators, built-ins, limits
Architecture
Module map, file counts, conventions
"Every line of Rust that emits ARM64 assembly is annotated with an inline comment explaining what it does and why."
From stack frame setup to syscall invocation, from integer-to-string conversion to array memory layout.
If you've ever wondered what happens between echo "hello" and the CPU executing it,
follow the code from src/codegen/ and read the comments.
Get started
Install the
PHP compiler
elephc is written in Rust and targets Apple Silicon. Three commands to install the compiler and compile your first PHP file to a native binary.
Requirements
cargo) Project structure
Roadmap
0.1.0 Basic CLI compiler 0.2.0 Arrays and null 0.3.0 Bool, float, type system 0.4.0 Strings 0.5.0 I/O and file system 0.6.0 Assoc arrays, switch, match current 0.7.0 Constants, closures, variadic 0.8.0 Date/time, JSON, regex 0.9.0 Multi-platform, optimizations 1.0.0 Production-ready