v0.6.0 — associative arrays & switch/match

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.

terminal
$ elephc hello.php
$ ./hello
Hello, World!
$ _
~750 tests
100+ built-in functions
6 data types
0 runtime deps

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.

Aa
01

Lexer

Character-by-character scanning of PHP source into keywords, operators, literals, and identifiers.

{}
02

Parser

Pratt parser with binding powers builds a structured tree of expressions and statements.

>>
03

Resolver

Processes include, require, include_once, and require_once — merging multiple files into one AST.

T:
04

Type Checker

Infers and validates types for every variable and expression. Catches errors before assembly is emitted.

asm
05

Codegen

Every AST node is translated to annotated ARM64 instructions. Each line is commented explaining what and why.

ld
06

Link

macOS as assembles to object code, then ld links into a standalone executable. Zero dependencies.

.php tokens AST typed AST .s .o binary

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

Echo / Print
Variables & assignment
Arithmetic: + - * / % **
Comparison: == != < > === !==
Logical: && || !
String concatenation (.)
Assignment: += -= *= /= .= %=
Pre/post increment/decrement
Type casting: (int) (float) (string) (bool) (array)
Ternary operator
If / elseif / else
While / do-while
For / foreach
Switch / match
Break / continue
Functions with recursion
Include / require / once
String interpolation
Comments: // and /* */

Compile-time error messages

error[3:1]: Undefined variable: $x
error[5:7]: Type error: cannot reassign $x from Int to Str
error[2:1]: Required file not found: 'missing.php'

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.

hello.php
Hello World
1 <?php
2 echo "Hello, World!\n";
fibonacci.php
Fibonacci
1 <?php
2 function fib($n) {
3 if ($n <= 1) {
4 return $n;
5 }
6 return fib($n - 1) + fib($n - 2);
7 }
8
9 for ($i = 0; $i <= 20; $i++) {
10 echo fib($i) . "\n";
11 }
fizzbuzz.php
FizzBuzz
1 <?php
2 $i = 1;
3 while ($i <= 100) {
4 if ($i % 15 == 0) {
5 echo "FizzBuzz\n";
6 } elseif ($i % 3 == 0) {
7 echo "Fizz\n";
8 } else {
9 echo $i . "\n";
10 }
11 $i++;
12 }
primes.php
Prime Checker
1 <?php
2 function is_prime($n) {
3 if ($n <= 1) return false;
4 $i = 2;
5 while ($i * $i <= $n) {
6 if ($n % $i == 0)
7 return false;
8 $i++;
9 }
10 return true;
11 }

All 25+ included examples

hello arithmetic arrays assoc-arrays cli-args concat control-flow countdown factorial fibonacci file-io fizzbuzz float-math functions guess-game logical multi-file nested-arrays primes strict-compare string-builder string-ops switch-match type-ops variables

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
Debug: var_dump print_r
System: exit die
Constants: 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.

"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

Rust toolchain (cargo)
Xcode Command Line Tools
macOS on Apple Silicon (ARM64)
1. Clone & build
$ git clone https://github.com/illegalstudio/elephc.git
$ cd elephc
$ cargo build --release
2. Compile & run
$ elephc hello.php
$ ./hello
Hello, World!
3. Run the tests
$ cargo test
# ~750 tests, all passing

Project structure

src/
main.rs — CLI entry point
lexer/ — source → tokens
parser/ — tokens → AST
types/ — static type checking
codegen/ — AST → ARM64
builtins/ — one file per function
strings/ arrays/ math/ types/ io/ system/
runtime/ — ARM64 routines
strings/ arrays/ io/ system/
errors/ — line:col formatting

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