Learn how to compile PHP extensions from its source code using Visual Studio in a Windows environment.
After working on a very old project based on PHP 5.3, where the APC library was required to make it work, i knew how difficult is to find the APC extension to make it work on the Windows platform. For Linux, the process can be pretty easy with PECL, however in Window this was a real problem.
That's why today i'm going to show you how to compile a PHP extension from its source code from scratch in a Windows environment, specifically with the APC library and PHP 5.3.8.
- Visual Studio: the version needs to provide some compatibility with the accepted versions of Visual C++, see the first point for more information.
- PHP Source Code: The source code of PHP for Windows at the version that you need to be compatible with the extension.
- Extension Source Code: The source code of the extension for PHP that you want to compile into a dynamic link library (DLL file).
Having said that, let's get started with the compilation !
1. Prepare Visual Studio environment
Before getting started with the compilation of a PHP extension, you need to know that not every VS compiler is compatible with any version of PHP, so you will need to know which Visual Studio you need to use according to the version of PHP that you want compile your extension (obtain DLL file). Check out the following table that specifies which version of Visual Studio you need to compile your desired PHP version:
- Yes: this version is supported and the PHP team provide binaries built with this compiler
- No: Not supported
|Visual C++ 6 (SP6 only)
|Visual C++ 7/7.1 (2002, 2003, 2003+sp1)
|Visual C++ 8 (VS 2005)
|Visual C++ 9 (VS 2008 SP1 only)
|Visual C++ 11 (VS 2012)
|Visual C++ 14 (VS 2015)
|Visual C++ 15 (VS 2017)
- (*) May be dropped before the final release
- (**) Not officially supported but known to work
- (***) Supported for this given compiler according to the current official support (or known to work)
So to make it easy to understand, if you are willing to compile an extension for PHP 7, you will need the source code of PHP 7, the code of the extension as well and at least Visual Studio 2012 assuming that you want to compile it for PHP 7.0, because if you want it for PHP 7.1, then you would need at least Visual Studio 2015.
Knowing this, if you are sure that you have the correct version you shouldn't face any trouble while you compile the extension. Otherwise you will face weird exceptions during the configuration of the build:
Saving configure options to config.nice.batChecking for cl.exe ... <in default path>Detected compiler undefinedC:\php-src\configure.js(1817, 9) Microsoft JScript runtime error: 'length' is null or not an object
In this tutorial we'll build the APC extension for PHP 5.3.8, so in our case we'll need to work with Visual Studio 2008 SP1 for visual c++ 9.
2. Open Visual Studio Command Prompt
In order to run some administrative commands related to the development and build of your extension, you will need a command prompt with the context of Visual Studio. Namely theDeveloper Command Prompt for VS that you can find at the path:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\<Your Visual Studio Version e.g 2008 or 2017>\Visual Studio Tools
Here you will find an executable of the command prompt that has access to all the tools that you will need to build the extension, execute it with administrator rights:
As mentioned, is a normal command prompt that contains the tools that we need for the next steps:
Let it open as we'll need in the step 5.
3. Download PHP Source Code
Now that you have the necessary tools to compile PHP along with your extension, you will need as well the source code of PHP. You can get the source code of the PHP version that you need from the official releases webpage of PHP here.
Once you download the tar/zip file with the code, extract it into a directory with a short path to prevent any problem with the length of the paths in Windows. In our case, we'll extract the source code of PHP 5.3.8 in the
We'll run the commands to build PHP along with your code in this directory within the command prompt later. It's worth to mention that the ext folder will contain the code of your extension but we will add it in the next step.
4. Prepare Extension Source Code
In order to build your extension, either you build an open source extension or create your own extension. In our case, we will build the APC extension, the version 3.1.6 specifically for PHP 5.3.8 (the same version of our PHP source code). This will ensure that the generated dll of PHP will be compatible with the PHP version that we need.
Once you download the source code of your extension (or you write your own code for it), extract it inside the
ext folder of the PHP source code. In our case, the directory for the extension would be
c:/php-src/ext/apc and it will contain the code of the extension (this is very important as the extension will be compiled along as a dynamic library with PHP):
Be sure as well that the
config.w32 file in the root directory of the source code of the extension, in the line that registers the extension with the EXTENSION function, the third parameter is set to true in order to generate a DLL (dynamic mode):
EXTENSION('apc', apc_sources, true);
Because as you may know you can integrate the extension directly with PHP, but in this tutorial we are showing how to generate the DLL file that in our case will be
5. Configure PHP and Extension build
Go back to the visual studio developer command prompt and switch to the directory of the source code of PHP:
Then run the
buildconf.bat file with the
This will generate a new .bat file namely
configure.bat that will have the configuration of your PHP build. Run the bat with the following command and enable your extension using the
--enable-<extension_folder_name> argument. In our case, the extension is APC and we stored the source code in the
c:/php-src/ext/apc directory, so the extension name will be APC. Run the command to configure it:
configure --disable-all --enable-cli --enable-apc
If you run the previous command and you face an exception because bison.exe haven't been found:
- Saving configure options to config.nice.bat
- Checking for cl.exe ... <in default path>
- Detected compiler MSVC9 (Visual C++ 2008)
- Detected 32-bit compiler
- Checking for link.exe ... C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN Checking for nmake.exe ... <in default path>
- Checking for lib.exe ... <in default path>
- Checking for bison.exe ... <not found>
- ERROR: bison is required
Download the binaries of Bison from the website at Source Forge here. Then paste the bison.exe and m4.exe at the bin directory of Visual Studio e.g
C:\Program Files (x86)\<Your Microsoft Visual Studio Version e.g 9.0>\VC\bin. This will make the bison executable accesible to the path and the exception bison is required won't appear anymore.
--disable-all flag will help us to prevent any other exception of extensions that we don't need, just focusing on the APC extension that we need. And that's it, you will get a verbose output that includes a list with all the extensions that will be built along with PHP and its mode e.g Static (built-in classes in PHP) or Shared (a Dynamic Link Library dll):
As expected, our extension of PHP will be built into a DLL file just as we want it. By default, this will create a "Thread Safe" build (PHP 5.3.8 TS). If you want a non thread safe build, just add the
--disable-ztsflag to the configure command.
6. Build PHP and Extension
Finally, run the nmake command to start the build of your extension with PHP within the directory of the PHP source code with the developer command prompt.NMake is a make utility provided by Microsoft and available in Visual Studio. It is a handy tool for creating automated builds.
Just run the command:
This will start the compilation and will take a while. It will generate a verbose output as well with warnings etc:
Once it finishes, a new directory will appear inside the
c:/php-src directory, namelyRelease orRelease_TS according to the Thread Safety configuration. There you will find a very basic built of PHP, but most important, the reason why you exclusively built PHP, your extension DLL file (in our case
This article is a recopilation of multiple sources that you may find useful as well during the compilation of an open source extension or your own extension:
Happy coding !
How to compile PHP on Windows? ›
This walkthrough covers these tasks:
- Create a DLL project in Visual Studio.
- Add exported functions and variables to the DLL.
- Create a console app project in Visual Studio.
- Use the functions and variables imported from the DLL in the console app.
- Run the completed app.
How to create a DLL file in PHP? ›
Compiling and building ¶
- Open a command prompt which is used to build PHP.
- Go to the root folder where PHP sources are present.
- Run the command: cscript.exe win32\build\buildconf.js.
- Run the command: configure.bat --help. ...
- Run the command: configure.js [all options used to build PHP] --enable-wincache. ...
- Run the command: nmake.
How to create PHP File in Visual Studio? ›
Having said that, let's get started with the compilation !
- Prepare Visual Studio environment. ...
- Open Visual Studio Command Prompt. ...
- Download PHP Source Code. ...
- Prepare Extension Source Code. ...
- Configure PHP and Extension build. ...
- Build PHP and Extension.
Can Visual Studio run dll files? ›
Create your PHP project
- Select Create a new project from the start page or File > New > Project… from the title bar.
- In the language combo box select PHP, select PHP Web Project, then select Next.
- Name your project and select Create.
- At this point, you can select the project template.
The Microsoft Windows Visual Studio is a program that allows you to view, edit and build code into a DLL file.
Can Visual Studio open dll files? ›
A DLL (dynamic-link library) is a library that contains code and data that can be used by more than one app. You can use Visual Studio to create, build, configure, and debug DLLs.
Is there a way to compile PHP? ›
phc allows you to compile PHP programs into shared libraries, which can be uploaded to the server. The PHP program is compiled into binaries. It's done in such a way as to support eval s, include s, and the entire PHP standard library.
How to compile a PHP file? ›
How do I run and compile a PHP program? ›
Preparing environment: packages necessary to compile PHP from source
- gcc. GCC (GNU C Compiler) is an open source C compiler, widely used by most C projects out there. ...
- libc-dev. ...
- autoconf. ...
- bison. ...
- re2c. ...
- make. ...
- Install required packages. ...
- Build the configuration script.
To run PHP for the web, you need to install a Web Server like Apache and you also need a database server like MySQL. There are various web servers for running PHP programs like WAMP & XAMPP. WAMP server is supported in windows and XAMP is supported in both Windows and Linux.
Can we use DLL in PHP? ›
If this is a simple DLL there is no way yet to run it from PHP. If the DLL contains a COM server you may be able to access it if it implements the IDispatch interface.
How to create a DLL file in command prompt? ›
- Step 1: Download the PHP files. Get the latest PHP x64 Thread Safe ZIP package from https://windows.php.net/download/.
- Step 2: Extract the files. ...
- Step 3: Configure php.ini. ...
- Step 4: Add C:\php to the PATH environment variable. ...
- Step 5: Configure PHP as an Apache module. ...
- Step 6: Test a PHP file.
Can you develop PHP in Visual Studio? ›
Step to create DLL at the Dos Command Prompt
- Run Dos Command Prompt.
- Create a working directory that contains the necessary files see above.
- Run f90 /dll myfile.for solver.lib (if the source file name is myfile.for)
PHP in Visual Studio Code. Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions.
How to format PHP code in Visual Studio? ›
How to set PHP path in Visual Studio Code? ›
The formatting can be triggered explicitly by two actions:
- Format Document ( Shift+Alt+F ) - Format the entire active file.
- Format Selection ( Ctrl+K Ctrl+F ) - Format the selected text.
Open the VS Code settings by going to Preferences > Settings (on macOS) or File > Preferences > Settings (on Windows). In the search box, type "php. validate. executablePath" and select the "Edit in settings.
How to create DLL file in Visual Studio Code? ›
Write a Sample DLL
How to add DLL file in Visual Studio Code? ›
In Microsoft Visual C++ 6.0, you can create a DLL by selecting either the Win32 Dynamic-Link Library project type or the MFC AppWizard (dll) project type. The following code is an example of a DLL that was created in Visual C++ by using the Win32 Dynamic-Link Library project type.
Use Project -> Add Project Reference. Then in dialog window navigate to Browse tab and use Browse to find and attach your dll.
How to embed DLL in Visual Studio? ›
Just right-click your project in Visual Studio, choose Project Properties -> Resources -> Add Resource -> Add Existing File… And include the code below to your App.
How to decompile a DLL in Visual Studio? ›
To do this, go to the Modules window and from the context menu of a . NET assembly, and then select the Decompile Source to Symbol File command. Visual Studio generates a symbol file for the assembly and then embeds the source into the symbol file. In a later step, you can extract the embedded source code.
How to test a DLL in Visual Studio? ›
If the DLL project exports the functions that you want to test, then you can add a reference to the code project from the test project.
- Create a Native Unit Test Project. ...
- In Solution Explorer, right-click on the test project, then choose Add > Reference.
- Select Projects, and then the project to be tested.
Why PHP is not compiled? ›
- Applies to: Visual Studio Visual Studio for Mac Visual Studio Code.
- During Visual Studio debugging, the Modules window lists and shows information about the DLLs and executables (.exe files) your app uses.
- To open the Modules window, while you're debugging, select Debug > Windows > Modules (or press Ctrl + Alt + U).
PHP is an interpreted language. It can be compiled to bytecode by third party-tools, though. "bytecode" is a term used for VM specific pseudo instructions, which are not native hence can't be regarded as "compilation" in this context. "object code" is just bytecode for the CPU's instruction decoder.
Is PHP applications Cannot be compiled? ›
A php applications cannot be compiled b php cannot be
How to compile PHP in command prompt? ›
To define a constant you have to use define () function.
all you need is just to write php index. php if index. php is your file name. then you will see that, the file compiled and showing it's desired output.
Which server is used to compile PHP code? ›
Apache web server
How to run PHP file step by step? ›
The -dev package is required in order to build PHP.
How does PHP compilation work? ›
Executing PHP files ¶
- Tell PHP to execute a certain file. $ php my_script.php $ php -f my_script.php. ...
- Pass the PHP code to execute directly on the command line. $ php -r 'print_r(get_defined_constants());' ...
- Provide the PHP code to execute via standard input ( stdin ).
Compilation. Without the use of JIT, PHP in its standard form is compiled from the generated AST to OPCode, not – as is the case with JIT – into machine code. The compilation process is carried out by recursively traversing the AST, as part of which some optimisations are made as well.
How to run PHP project from command line in Windows? ›
What is the extension of PHP file? ›
- Download it from the official site.
- Extract the file and store it in a folder, preferably named php , in your C just like so: c:/php.
- Next, place this folder in your environment variable path so that it can be recognized by the command line.
php file extension refers to the name of a file with a PHP script or source code that has a ". PHP" extension at the end of it. It's similar to a Word file with a . doc file extension.
Can you run PHP from command line? ›
Although the PHP scripting language has been mainly developed for the creation of dynamic websites, you can also execute PHP scripts completely independent of the web server. For this purpose, you can call the PHP script on the command line with the so-called PHP Command Line Interpreter (short: PHP CLI).
Copy the php_wincache. dll file into the PHP extensions folder. Typically this folder is called "ext" and it is located in the same folder with all PHP binary files. For example: C:\Program Files\PHP\ext .
What program runs PHP files? ›
Amongst them, PHP XAMPP and WampServer are the most popular. While WampServer is only available for the Windows platform, XAMPP is a cross-platform application that can run on Windows, Linux, and macOS. Hence, in this tutorial, you will learn PHP using XAMPP.
Can a DLL be an executable? ›
It is not possible to directly execute a DLL, since it requires an EXE for the operating system to load it through an entry point, hence the existence of utilities like RUNDLL.
How to install PHP compiler in Windows 10? ›
Install PHP on Windows manually
What is the easiest way to install PHP on Windows? ›
You have to download zipped binary version of PHP from http://www.php.net/downloads.php. Extract the downloaded file to php folder which must be created at the root of any of your windows drives (e.g. c:\php or d:\php). Enter php folder and rename the file php. ini-recommonded to php.
The fastest and easiest way to install PHP on Internet Information Services (IIS) is by using the Microsoft® Web Platform Installer (Web PI). Web PI completely automates setting up IIS, FastCGI, and the latest version of PHP from the php.net Web site.
Where to create PHP script? ›
A PHP script can be placed anywhere in the document. The default file extension for PHP files is " .php ". A PHP file normally contains HTML tags, and some PHP scripting code.
How is DLL file generated? ›
In Visual C++ 6.0, you can create a DLL by selecting either the Win32 Dynamic-Link Library project type or the MFC AppWizard (dll) project type. The following code is an example of a DLL that was created in Visual C++ by using the Win32 Dynamic-Link Library project type.
How to register DLL in Visual Studio command prompt? ›
Click Start > All Programs > Accessories and right-click on "Command Prompt" and select "Run as Administrator" OR in the Search box, type CMD and when cmd.exe appears in your results, right-click on cmd.exe and select "Run as administrator" At the command prompt, enter: REGSVR32 "PATH TO THE DLL FILE"
How to register DLL in Windows 10 command prompt? ›
How to install PHP extension in Visual Studio? ›
How to Register DLL Files on Windows?
- Press Win+R to open Run.
- Type the reg DLL command: regsvr32 “[the path of the DLL file]”. The following is an example:
- Click OK to execute the reg DLL command.
- You will receive a confirmation message once the DLL file has been successfully registered.
If you already have Microsoft Visual Studio installed, you can jump directly to the Manage Extensions dialog and search for PHP Tools for Visual Studio. Once downloaded, you'll be prompted to close Visual Studio. The package will be installed and Visual Studio configured for the PHP development.
How to write code to file PHP? ›
How To Install VSCode For PHP On Windows
- Step 1 - Download Visual Studio Code. We can download Visual Studio from the official download page. ...
- Step 2 - Install Visual Studio Code. ...
- Step 3 - Marketplace. ...
- Step 4 - Install Extensions. ...
- Step 5 - Getting started with PHP - Hello World.
PHP Write to File - fwrite()
How to open PHP file in browser using Visual Studio Code? ›
The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.
How to format code in Visual Studio command? ›
Open PHP/HTML/JS In Browser
- Click the button Open In Browser on StatusBar.
- In the editor, right click on the file and click in context menu Open PHP/HTML/JS In Browser.
- Use keybindings Shift + F6 to open more faster (can be changed in menu File -> Preferences -> Keyboard Shortcuts )
In an existing project, open the document that you want to format, right-click inside the document, and select Format Document. In the default configuration for Visual Studio Code, the command can be run using the shortcut Alt+Shift+F.
How to get PHP path to file? ›
In order to get the root directory path, you can use _DIR_ or dirname(). echo dirname(__FILE__); Both the above syntaxes will return the same result.
How to check PHP version in Visual Studio Code? ›
1. Type the following command, replacing [location] with the path to your PHP installation. 2. Typing php -v now shows the PHP version installed on your Windows system.
How to add PHP extension to path? ›
How to compile DLL from source code? ›
Installing the PHP extension on Windows
- Locate the php. ini file for your PHP installation, and open it in a text editor. ...
- Copy the file php-5. x.y_sqlanywhere. ...
- Add the following line to the Dynamic Extensions section of the php. ...
- Make sure that the Bin32 subdirectory of your SQL Anywhere installation is in your path.
How to run a DLL in Visual Studio C#? ›
- you need to install Visual Studio.
- need to install SKSE source code and add it as a reference properly to Visual Studio.
- add the dll source code as a project to Visual Studio (either by using git and/or releases if available)
- compile, test, and debug.
- copy dll to the correct Skyrim game folder.
- test in game.
How to compile exe with dll? ›
Using DLL File
- Open Visual Studio then select "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows Forms application".
- Design the form as in the following image:
- Add a reference for the dll file, "calculation. ...
- Select the DLL file and add it to the project.
- Right click on the project and choose properties.
- Got to the Publish Tab.
- Click on Application files.
- Make sure the DLL is in the Download Group ,"Required"
Is a DLL compiled code? ›
You cannot just simply convert any . dll file to program executable .exe.
- open solution.
- right-click on project you want to compile into exe -> Properties.
- Configuration Properties -> General.
- In Project Defaults change Configuration Type to Application(.exe)
A . dll file contains compiled code you can use in your application.
How do I decode a DLL file? ›
In order to see the code that makes a DLL file work, you will need to use a decompiler to revert it back into readable code. Opening a DLL file without a decompiler (such as opening it with Notepad) will result in a jumbled mess of unreadable characters. dotPeek is one of the more popular free decompilers.
How to compile source code Visual Studio? ›
Create a New Project
How to add DLL to Visual Studio code? ›
Enter a name and location for your project and select 'Create'. Visual Studio will then generate the necessary project files, and you can add code to your project. Once your project is set up, you can compile your code by pressing the “Build” button.
Simply #include the DLL header files in your C++ code, and then add the DLL import . lib files to your Project. Or, you can load the DLL functions dynamically at runtime via GetProcAddress() if you don't have . lib files (though you can easily generate them from the DLLs if needed).
What is the difference between .EXE and .DLL files? ›
Is a DLL file an EXE file? ›
The EXE will create its own thread and reserve resources for it if you run it. A DLL file, on the other hand, is an in-process server, so you cannot run a DLL file on its own.
Difference between EXE and DLL.
5 more rows
|Basis of Comparison
|An exe is a program
|DLL is a library
Aug 4, 2022
A DLL file is not by it self executable, though it may contain executable code. A DLL (Dynamic-Link Library) contains code, data, resources etc. usable by other programs. You need an EXE file for the operating system to execute code within DLL files, like "RUNDLL.
What is DLL file extension? ›
A dynamic link library (DLL) is a collection of small programs that larger programs can load when needed to complete specific tasks. The small program, called a DLL file, contains instructions that help the larger program handle what may not be a core function of the original program.