PHP's Foreign Function Interface (FFI) allows calling C functions and manipulating C data structures directly from PHP. This enables integration with system libraries, native performance for critical paths, and access to OS-level APIs.
⚠️ Note: FFI requires PHP compiled with --with-ffi. The standard php:8.3-cli Docker image does not include FFI. Use php:8.3-fpm or build PHP with FFI support. To verify: php -m | grep FFI
Step 1: FFI Setup & Prerequisites
# Check FFI availabilityphp-m|grepFFI# If FFI is available:# FFI# Enable in php.ini:# ffi.enable=1 (default: preload — only allow in preloaded scripts)# ffi.enable=2 (true — allow everywhere, development only)# For Docker: use php:8.3-fpm which includes FFIdockerrun-it--rmphp:8.3-fpmbash-c"php -m | grep FFI && echo 'FFI available'"
💡 FFI security model:ffi.enable=preload (default in PHP-FPM) only allows FFI in opcache.preload scripts. Use ffi.enable=true for CLI/development. Never use true in production web contexts.
Step 2: FFI::cdef() — Basic libc Calls
📸 Expected Output (with FFI enabled):
Step 3: FFI Structs & Pointers
Step 4: FFI qsort — Callback Functions
📸 Expected Output:
Step 5: FFI::load() — Header Files
Step 6: Performance Comparison
💡 FFI overhead: Each FFI call has ~100-200ns overhead for type marshaling. For functions like sin(), PHP's built-in is faster. FFI is best for: bulk data processing, manipulating C structs, calling complex library functions not exposed in PHP.