PHP WASM Builder 1.0: Compile PHP to WebAssembly for Browser and Node.js
php-wasm-builder 1.0 enables compiling PHP with custom versions and extensions into a single WebAssembly module that runs in browsers and Node.js without a PHP server, demonstrated by running Symfony apps and PHP libraries entirely client-side via static hosting.
Reading font

What It Does
php-wasm-builder compiles PHP into WebAssembly, allowing you to select a PHP version (8.3–8.6) and extensions (ctype, mbstring, dom, intl, etc.). The resulting .wasm file runs in a browser or Node.js without any PHP server. A minimal example loads php-web.mjs, instantiates the PHP runtime, and executes echo "Hello from PHP ", PHP_VERSION; printing Hello from PHP 8.6.0.
Real-world PHP libraries work unchanged: JoliTypo fixes typography, JoliMarkdown sanitizes Markdown, and structured-data validates schema.org. Entire Symfony applications run, including the Symfony Demo with Doctrine, SQLite, EasyAdmin backend, and 30 programming languages — all served as static files on GitHub Pages with zero server cost and no data leaving the browser tab.
How It Works
WebAssembly targets a virtual instruction set; the browser translates it to native CPU instructions at load time. The module runs in a sandbox with no direct file or network access — it only calls JavaScript-provided functions.
PHP is a large C program. Emscripten replaces GCC, and its JavaScript glue layer acts as an OS kernel: when PHP writes to stdout, the glue captures bytes; when PHP opens a file, the glue reads from an in-memory filesystem. PHP communicates via SAPIs: cli for terminal, fpm for nginx, and the lesser-used embed SAPI that turns PHP into a library callable from C. Here the host program is npm run.c run dev, which starts PHP, runs your code, and stops — each run is a fresh request with no shared state.
Extensions like mbstring (needs oniguruma), dom (needs libxml2), and intl (needs ICU) must also be compiled to WebAssembly. Everything links into a single .wasm file — no .so loading.
Build Process
Compiling all dependencies requires the correct Emscripten version and cross-compilation knowledge. php-wasm-builder packages everything in a Dockerfile: a first stage converts requested extensions into a build plan (only oniguruma and libxml2 if you add mbstring and dom; ICU only if you add intl), then subsequent stages build each library and finally PHP. Docker caching avoids rebuilds.
Using It in Your Project
Install via Castor: castor composer require lyrixx/php-wasm-builder. Define a stack in castor.php:
use PhpWasm\Stack;
use PhpWasm\Target;
function wasm_stack(): Stack {
return Stack::named('app')
->php('8.6.0')
->extensions('ctype', 'mbstring')
->embed('build/embed', at: '/app')
->targets(Target::Web)
->outputDir('public/build');
}PHP cannot see your disk; it only sees the directory created inside embed(). You must prepare that directory: copy source, composer.json, composer.lock, run composer install --no-dev --classmap-authoritative inside it, then invoke the builder.
#[AsTask('build', namespace: 'wasm')]
function wasm_build(): void {
$embed = __DIR__.'/build/embed';
fs()->remove($embed);
fs()->mirror(__DIR__.'/src', "{$embed}/src");
fs()->copy(__DIR__.'/composer.json', "{$embed}/composer.json");
fs()->copy(__DIR__.'/composer.lock', "{$embed}/composer.lock");
run(['composer', 'install', '--no-dev', '--classmap-authoritative'],
context: context()->withWorkingDirectory($embed));
build([wasm_stack()]);
}Run castor wasm:build. Outputs php-web.mjs and php-web.wasm in public/build/. In the browser:
const {exitCode, stdout} = await php.runFile('/app/src/page.php', {stdin: 'Hello...'});Input goes via stdin, output via stdout. Written data persists in IndexedDB per site (Symfony Demo stores SQLite there). The docs also show Composer+Docker usage without Castor, including Node.js tests and GitHub Pages deployment.
Size and Performance
PHP 8.6 with default extensions: 8.7 MB (3.1 MB gzipped).
Symfony Demo with vendor: 7.2 MB gzipped.
intl variant (ICU with all locale data): 16.4 MB gzipped.
Running Symfony
Symfony expects HTTP requests, not scripts. php.request() runs PHP like a web server, returning status, headers, and body. But browsers request many assets (links, forms, redirects, stylesheets). GitHub Pages only serves static files. The solution: a Service Worker intercepts all requests and forwards them to PHP.
Two integration functions: serveInFrame() — app runs in an <iframe>; Service Worker passes requests to the page, PHP runs in a Web Worker (off main thread, no UI jank). serveInServiceWorker() — app on a separate page, PHP runs inside the Service Worker. Looks like a real site with address bar and history. Caveat: Chrome stops idle Service Workers after 30 seconds; next request restarts it (database survives in IndexedDB). Builder forwards cookies and Referer hidden by the browser.
Challenges and Fixes
Stack overflow: Emscripten default C stack 64 KB; Symfony container compilation recursed 400 levels, corrupting memory silently. Fixed by increasing stack to 1 MB.
Memory leak after ~1900 requests: PHP's libc malloc relies on process exit to free memory. Switched to Zend MM which allocates its own memory and respects memory_limit.
Slow cold starts: Symfony recompiled container on every request. Added build-time warmup: bin/console cache:warmup runs in Node.js during Docker build. Opcache saves opcodes to files (no shared memory). Result: Node.js request latency dropped from 115 ms to 35 ms.
PHP bugs: Three bugs found during development; two fixed in PHP 8.5.13, third reported with a one-line patch (merged without attribution, but credited in changelog).
Limitations
32-bit module: PHP_INT_MAX = 2,147,483,647; timestamps stop at 2038.
No native network: curl, Symfony HttpClient, Guzzle need JavaScript fetch() to pass data via stdin.
No processes: proc_open() and exec() fail (Symfony Process warns).
Fibers kill the module because WebAssembly cannot suspend/resume stacks.
Version 1.0 Guarantees
Versions 0.1.0 (2026-10-20) to 0.14.0 (2026-11-20) saw 14 releases in a month with breakages. 1.0.0 promises semantic versioning: build parameters, default extensions, output files, and JavaScript Stack API will not change in minor releases. PHP, Emscripten, library versions, and file sizes may still update. Pre-release audit included: php.request() now behaves like a production web server (fatal errors → 500, errors to console, runs index.php for directories, populates PATH_INFO). Dockerfile verifies downloads. Examples run in real Chrome in CI with Service Worker and IndexedDB (Node.js lacks these).
For questions about attribution, corrections or content rights, please contactand we will review it promptly.