Using FFI in an Application
Explore how to use PHP 8's Foreign Function Interface (FFI) to incorporate external C libraries into your PHP applications. Learn the process of compiling C code into shared libraries and invoking C functions, such as a bubble sort algorithm, directly within PHP scripts. This lesson helps you understand the steps and code necessary to extend PHP capabilities with C language integration for advanced programming tasks.
We'll cover the following...
Any shared C library (generally with a *. so extension) can be included in a PHP application using the FFI extension. If we plan to work with any of the core PHP libraries or libraries produced when PHP extensions are installed, it’s important to note that we have the ability to modify the behavior of the PHP language itself.
Before we examine how that works, let’s first have a look at incorporating an external C library into a PHP script using the FFI extension.
Integrating an external C library into a PHP script
For the purposes of illustration, we use a simple function that might have originated from a Computer ...