This article will introduce to you the functions that allow PHP to call C: FFI extension. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
#In large companies, there are usually many programming languages. For example, let Java be used as the microservice layer, C be used for underlying operations, PHP be used as the middle layer, and finally JS can be used to display the effect. Most of the cooperation between these languages is completed through RPC, or the data is directly stored in the database and then retrieved using different languages. So, can our PHP code directly call these languages? In fact, PHP has really prepared an extension library for us that can directly call the C language, and this extension library is already built into PHP by default. It is the FFI extension.
What is FFI
FFI, Foreign Function Interface, external function interface. This extension allows us to load some public libraries (.dll, .so), which actually means that we can call some C data structures and functions. It is already an extension released with the PHP source code. You can add --with-ffi when compiling to compile it directly into the PHP program.
We already have compiled PHP here, so we find this extension directly and perform simple extension installation steps to complete the installation.
cd php-7.4.4/ext/ffi/ phpize ./configure make && make install
After the installation is complete, remember to open the extension in the php.ini file. One thing to note about this extension is that it has a configuration item ffi.enable. By default, the value of this configuration item is "preload", which only enables FFI capabilities in the CLI SAPI environment. Of course, we can also change it to "true" or "false" to turn it on and off. Setting to "true" will enable this extension in all environments.
Use FFI to call C functions
Next, let’s take a brief look at how it calls C functions.
// 创建一个 FFI 对象,加载 libc 并且导入 printf 函数 $ffi_printf = FFI::cdef( "int printf(const char *format, ...);", // C 的定义规则 "libc.so.6"); // 指定 libc 库 // 调用 C 的 printf 函数 $ffi_printf->printf("Hello %s!\n", "world"); // Hello World // 加载 math 并且导入 pow 函数 $ffi_pow = FFI::cdef( "double pow(double x, double y);", "libboost_math_c99.so.1.66.0"); // 这里调用的是 C 的 pow 函数,不是 PHP 自己的 echo $ffi_pow->pow(2,3), PHP_EOL; // 8
We created two objects and called C’s printf() and pow() functions respectively. FFI::cdef() is used to create an FFI object. It receives two parameters, one is a string containing the declaration sequence of regular C language (type, structure, function, variable, etc.).
Actually, this string can be copied and pasted from the C header file. The other parameter is the name of the shared library file to be loaded and linked. That is, the .dll or .so file we need, which corresponds to the string we declare. For example, there is no calculation function such as pow() in libc.so.6, so we have to find the math-related C Language calculation function library.
Define variables and arrays
Of course, FFI can also define variables and arrays.
// 创建一个 int 变量 $x = FFI::new("int"); var_dump($x->cdata); // int(0) // 为变量赋值 $x->cdata = 5; var_dump($x->cdata); // int(5) // 计算变量 $x->cdata += 2; var_dump($x->cdata); // int(7) // 结合上面的两个 FFI 对象操作 echo "pow value:", $ffi_pow->pow($x->cdata, 3), PHP_EOL; // pow value:343 $ffi_printf->printf("Int Pow value is : %f\n", $ffi_pow->pow($x->cdata, 3)); // Int Pow value is : 343.000000 // 创建一个数组 $a = FFI::new("long[1024]"); // 为数组赋值 for ($i = 0; $i < count($a); $i++) { $a[$i] = $i; } var_dump($a[25]); // int(25) $sum = 0; foreach ($a as $n) { $sum += $n; } var_dump($sum); // int(523776) var_dump(count($a)); // int(1024) 数组长度 var_dump(FFI::sizeof($a)); // int(8192),内存大小
Use the FFI::new() function to create a C data structure, that is, variable declaration. The contents of these variables will be saved in the cdata attribute. The array can directly operate on the return value of this function. Of course, when we want to end use, we still need to use FFI::free() to release the variables, just like C language development.
Summary
Doesn’t it feel very high-end? But please note that the C functions called by FFI are not as efficient as those called by PHP itself. For example, using the pow() function is more efficient using PHP itself. Moreover, although the FFI extension has been released simultaneously with PHP, it is still experimental. In other words, this extension is prepared for other functions that may be used in the future, and there are still many uncertainties. Therefore, if you need similar functions in a production environment, you still need to do more in-depth research.
Test code:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202004/source/%E8%AE%A9PHP%E8%83%BD%E5%A4%9F%E8%B0%83%E7%94%A8C%E7%9A%84%E5%87%BD%E6%95%B0-FFI%E6%89%A9%E5%B1%95.php
Recommended learning: php video tutorial
The above is the detailed content of Allow PHP to call C functions: FFI extension (with code). For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
