Home >Backend Development >PHP Tutorial >A simple example of writing a PHP extension in PHP 5.6 version_PHP Tutorial
This article mainly introduces a simple example of writing a PHP extension in php 5.6 version. This article gives the extension implementation code , compilation methods, configuration methods and usage examples, friends in need can refer to it
Sometimes when PHP itself does not have an API that meets the needs, you need to write the corresponding extension yourself. After the extension is written and compiled, you can add it to your own development environment and expand the functions of PHP.
Here is a simple extension of the connection operation that connects strings and int numbers.
First, download the latest php source code installation package, enter the ext/ directory, and create a new extstrcat.def:
The code is as follows:
The code is as follows:
The code is as follows:
At this time, edit ext/extstrcat/extstrcat.c and find the PHP_FUNCTION(extstrcat) function. This means that the method in the extension is called extstrcat. The method is implemented as follows:
The code is as follows:
, where strarg and intarg are the corresponding two string and integer parameters.
The next thing to do is to compile the extension,
The code is as follows:
Modify php.ini and add extension = extstrcat.so.
Restart php-fpm and run phpinfo() to see that the extstrcat extension has been added.
Now let’s write a Demo and test the php extension just now,