Part 2 : Writing PHP Extension
In 1st Part of this article, we already warmed up by preparing your development environment and grab the concept of PHP internal (including IIS 7.0). In this part I will guide you to write your first PHP extension (the simplest one), with VC++ 2008 in Windows Vista. Continuing the previous two steps in Part 1, lets go to step 3.
3. Setup Build Environment with VC++ 2008
I assume you have installed IIS 7.0 and VC++ 2008. After you’ve got PHP 5 installed and configured for your server, download and extract the complete PHP 5 source code, also from www.php.net. DO NOT extract the source archive over top of your existing binary installation. This article’s my PHP 5.2.6 binaries installed to C:\PHP, and its source codes extracted to D:\PHP on Windows\PHP-DEV\PHP-5.2.6.
In VC++ 2008, create a new Win32 project using the project template, name it FirstPHPExt. Select DLL app type in the VC++ Wizard and click finish. Open your C++ Project Property form and do the following changes in:
Configuration Properties > General
- Character Set --> Use Multi-Byte Character Set
Configuration Properties > C/C++ > General
- Debug Information Format --> Program Database (/Zi)
- Detect 64-bit Portability Issues --> No
- Additional Include Directories --> Root, Zend, TSRM, Regex, and Main

Configuration Properties > C/C++ > Preprocessor
Add : ZEND_DEBUG = 0; ZTS =1; ZEND_WIN32; PHP_WIN32
Note: Do not be tempted to change ZEND_DEBUG to 1, even for Debug build, this will prevent our extension from being loaded into the pre-built PHP binaries installed on your system.
Configuration Properties > Linker > General
- Additional Include Directories --> C:\PHP\Dev (where php5ts.lib located)
- Output Files --> $(OutDir)\MyPHP-Custom.dll

Configuration Properties > Linker > Input
- Additional Dependencies --> php5ts.lib
Ok, now we are ready to write extension codes.
4. Writing PHP Extension Codes
We will write a simple PHP extension to calculate the double up value of an inputted parameter. We will use this method in PHP user-space like this:
<?php
$value = 14;
$result = DoubleUp($value);
print "Calling DoubleUp($value) returned $result";
?>
Start with modifying stdafx.h header file to include PHP Extension headers.
#pragma once
/* PHP Zend Extension headers */
/* include zend win32 config first */
#include "zend_config.w32.h"
/* include standard header */
#include "php.h"
Then add the following codes to FirstPHPExt.cpp file:
#include "stdafx.h"
/* declaration of functions to be exported */
ZEND_FUNCTION(DoubleUp);
/* compiled function list so Zend knows what's in this module */
zend_function_entry FirstPHPExtModule_functions[] = {
ZEND_FE(DoubleUp, NULL)
{NULL, NULL, NULL}
};
/* compiled module information */
zend_module_entry FirstPHPExtModule_module_entry = {
STANDARD_MODULE_HEADER,
"FirstPHPExt Module",
FirstPHPExtModule_functions,
NULL, NULL, NULL, NULL, NULL,
NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
};
/* implement standard "stub" routine to introduce ourselves to Zend */
ZEND_GET_MODULE(FirstPHPExtModule)
/* DoubleUp function */
/* This method takes 1 parameter, a long value, returns the value multiplied by 2 */
ZEND_FUNCTION(DoubleUp){
long paramValue = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", ¶mValue) == FAILURE) {
RETURN_STRING("Bad parameters!", true);
}
paramValue *= 2;
RETURN_LONG(paramValue);
}
If you compile your project now, you will get the following error:
Error C3163: '_vsnprintf': attributes inconsistent with previous declaration
Don't be panic, this is a common error in compiling old C/C++ codes in VC++ 2008 (I found it when I compiled Squid and Winpcap with VC++ 2008). In our case, zend_config.w32.h contains a re-definition of snprintf and vsnprintf that already defined in stdio.h. Change zend_config.w32.h then you will not see that error again:
#if not defined(_MSC_VER) || defined(__MINGW32__)
#define snprintf _snprintf
#define vsnprintf _vsnprintf
#endif
After the building the project, we get MyPHP-Custom.dll file (depends to Output file configuration) that is ready to deploy.
4. Deploying the Extension
Place the resulted DLL (MyPHP-Custom.dll) to PHP extension directory, in this case C:\PHP\ext. If want to attach a debugger to this extension, you will also want to copy the .pdb files. After that, please configure PHP to load your extension DLL by modifying PHP.ini file, add "extension=MyPHP-Custom.dll". Once you’ve edited and saved php.ini, restart your Application Pool in IIS 7.0 then browse index.php web page to test the result. If you do not already have index page, just create one using the following text, and save it to one of your virtual directories:
<?php
phpinfo();
?>
Now browse the index.php and see that new extension should now be seen in the listing. If you are not lucky enough, please do some troubleshot :).
Then the latest step is to test our extension (calling DoubleUp). Just modify the index.php as below:
I perfectly get the right result in my browser :
Great... Now we understand how to build simple PHP extension using VC++ 2008 in Windows Vista. I will cover PHP extension debugging in other part of my article. What is left for the Part 3 of this article is explanation of many Zend functions appear in our codes. I will focus on Zend Framework in the Part 3 of this posting. I hope you will enjoy C++ codes like I did !
Hope this helps !!
Ciao - RAM