// ———————————————————————-
// Thinker, Infrastructure Overseer,
// Strategic Planner and Tech Operations
// —————————————–

Create a PHP extension (tutorial)

This is a simple tutorial to create a PHP extension in windows for both Linux and Windows.

It will require some basic knowledge of PHP and C and creating the following files:

  • config.m4
  • php_YOUR_EXT_NAME.h
  • config.w32
  • YOUR_EXT_NAME.stub.php


  • PHP 8.1 or higher
  • WSL (Windows Subsystem for Linux) or Linux
  • Git Bash and CLI (Command Line Interface) Windows Console
  • Visual Studio 2019 or newer (Community Edition is sufficient)
  • Basic knowledge of C programming

Step 1: Install the PHP SDK and PHP Source

  1. Download PHP SDK:

Go to the PHP SDK page on GitHub and download the latest PHP SDK for Windows.

  1. Setup PHP SDK:

Extract the PHP SDK to a preferred location, e.g., C:\php-sdk.

  1. PHP Source Code:

Clone the PHP source code or download it from the official PHP Downloads page.

cd C:\php-sdk

git clone -b PHP-8.1 https://github.com/php/php-src.git php-src

  1. Prepare the Build Environment:

Open the “Developer Command Prompt for VS 2019” as an administrator and navigate to the PHP SDK directory.

cd C:\php-sdk\php-src

Set up the environment for compiling:


Step 2: Prepare Your Extension Directory

  1. Create Extension Directory:

Inside the PHP source tree, navigate to the ext directory and create a new directory for your extension, e.g., hello.

cd ext

mkdir hello

cd hello

  1. Create config.w32:

This file is used to configure your extension. Create config.w32 with the following content:

// config.w32

ARG_ENABLE("hello",  "Enable Hello extension",  "no");


if (PHP_HELLO  !=  "no") { // if enabled

EXTENSION("hello",  "hello.c");


  1. Create config.m4:

This file is used to configure your extension. Create config.m4 with the following content:

PHP_ARG_ENABLE(hello,  whether  to  enable  the  hello  extension,

[  --enable-hello  Enable  hello  extension  support])


if  test  "$PHP_EMPTYORVALUE"  !=  "no";  then

PHP_NEW_EXTENSION(hello,  hello.c,  $ext_shared)


Step 3: Write Your Extension Code

  1. hello.c:

Create hello.c inside your extension directory with the following content:

#include "php.h"


// Declaration of a function



// Function argument info

ZEND_BEGIN_ARG_INFO(arginfo_hello_world,  0)



// List of functions provided by this extension

const zend_function_entry hello_functions[]  = {

PHP_FE(hello_world, arginfo_hello_world)




// Module entry

zend_module_entry hello_module_entry = {


"hello", // Extension name

hello_functions, // Function entries






// Implement standard module entry point



// Implementation of hello_world



RETURN_STRING("Hello, world!");


Step 4: Compile the Extension (For Windows)

  1. Generate configure script:

Back in the root of the PHP source directory, generate the configure script.


  1. Configure Build:

Configure PHP build with your extension enabled:

configure --disable-all --enable-cli --enable-hello=shared

  1. Compile PHP:

Now compile PHP and your extension:


Step 5: Compile the Extension (For Linux)

Prepare the Linux Environment

  1. Open your Linux distribution through the Start menu.
  2. Update and install essential packages:

sudo apt update && sudo  apt  upgrade

sudo apt install build-essential autoconf bison re2c php-dev libtool-bin

Compile Your Extension in WSL

  1. Transfer your extension source code to the Linux environment. You can do this via the Windows file explorer (the Linux file system is accessible under \\wsl$\<Distribution Name>\home\<username>\).
  2. Navigate to your project directory in the WSL terminal.
  3. Prepare the PHP build environment:




This will compile the extension to a .so file.

  1. Install the extension (if testing on Linux or preparing for deployment):

sudo make install

  1. Configure PHP to use the extension by adding extension=your_extension.so to your php.ini.

Step 6: Test the Extension

  1. Edit php.ini:

Add your extension to the php.ini file:


// or for Linux


  1. Run PHP:

You can test if your extension loaded correctly by running:

php -m

And to test function:

php -r "echo hello_world();"

This will build the hello_world function into PHP as a custom extension, callable like any built-in function. Adjust the function definitions and functionality per your actual extension requirements.







Let us know your opinion!