What is PHP FFI (Foreign Function Interface)?
Why Interviewers Ask This
This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.
Answer
The FFI (Foreign Function Interface) extension (PHP 7.4+) allows PHP to call C functions and use C data types and structures directly from PHP code without writing a PHP extension in C. Load a C library: $ffi = FFI::cdef("int printf(const char *format, ...);", "libc.so.6"); $ffi->printf("Hello %s\n", "world");. FFI enables: calling system libraries, using high-performance C algorithms, accessing hardware APIs, and interoperating with native code. It is significantly faster than implementing equivalent logic in pure PHP. Drawbacks: complex API, type safety responsibility falls on the developer, platform-dependent (library paths differ by OS), and security risk (direct memory access). FFI is primarily for performance-critical extensions, scientific computing, and system programming where a full PHP extension is impractical.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.