A lightweight, portable, embeddable implementation of PHP written in C.
PH7 is an in-process software library that implements a highly-efficient embeddable bytecode compiler and virtual machine for the PHP programming language. It allows host applications to compile and execute PHP scripts in-process, making PH7 to PHP what SQLite is to SQL.
This is a refactored version of the original PH7 repository, revived after years of dormancy with modern development practices and a focus on reliability.
- Lightweight: Keep the engine suitable for embedded systems and resource-constrained environments
- Portable: Maintain compatibility across platforms with minimal dependencies
- Reliable: Introduce extensive testing and state-of-the-art continuous integration
- Compatible: Refactor PH7's original idiosyncrasies to be more compatible with official PHP.
-
PHP 5.3 Constructs: Support for heredoc, nowdoc, goto, classes, anonymous functions, closures, and more
-
Extended Language Features:
- Function & Method Overloading
- Full Type Hinting
- Comma expressions
- String comparison operators (
eqandne) - Improved operator precedence
- Powerful object-oriented subsystem
- Complex expressions as default function argument values
- 64-bit integer arithmetic on all platforms
- Native UTF-8 support
-
Embeddable Architecture:
- Written in ANSI C
- Thread-safe and fully reentrant
- Multiple interpreter states can coexist without interference
-
Rich Built-in Library (470+ functions):
- XML parser with namespace support
- INI processor
- CSV reader/writer
- UTF-8 encoder/decoder
- ZIP archive extractor
- JSON encoder/decoder
- Random number/string generator
- Native File I/O for Windows and UNIX
makeThe default build produces executables in build/x86_64-linux-gnu/ (or your platform's equivalent).
#include "src/ph7/ph7.h"
#include <stdio.h>
static int output_consumer(const void *pOutput, unsigned int nLen, void *pUserData) {
printf("%.*s", nLen, (const char *)pOutput);
return PH7_OK;
}
int main(void) {
ph7 *pEngine;
ph7_vm *pVm;
const char *code = "<?php echo 'Hello, World!'; ?>";
// Initialize engine
ph7_init(&pEngine);
// Compile PHP code
ph7_compile_v2(pEngine, code, -1, &pVm, 0);
// Configure output handler
ph7_vm_config(pVm, PH7_VM_CONFIG_OUTPUT, output_consumer, 0);
// Execute
ph7_vm_exec(pVm, 0);
// Cleanup
ph7_vm_release(pVm);
ph7_release(pEngine);
return 0;
}Compile with:
makeThe project includes a standalone PHP interpreter:
# Run a PHP file
./build/x86_64-linux-gnu/phl examples/hello_world.php
# With script arguments
./build/x86_64-linux-gnu/phl script.php arg1 arg2See the examples/ directory for more usage examples:
ph7_intro.c- Basic embedding exampleph7_cgi.c- CGI-style executionph7_func_intro.c- Registering C functionsph7_const_intro.c- Defining constants
(Testing infrastructure is being developed as part of this refactor)
# Run tests
make testPH7 is licensed under the BSD 3-Clause License. See LICENSES/ for details.
PH7 was originally developed in 2011 for embedded router web interfaces, providing a dynamic scripting environment in resource-constrained devices. This refactored version aims to modernize the codebase while preserving its lightweight, embeddable nature.
Contributions are welcome! This refactor focuses on:
- Improving test coverage
- Setting up modern CI/CD pipelines
- Maintaining backward compatibility
- Keeping the codebase portable and lightweight
This refactored version maintains API compatibility with the original PH7 while introducing:
- Modern build system improvements
- Comprehensive test suite
- Continuous integration
- Better documentation
- Active maintenance
Note: For the historical README, see OLDREADME.md.