Creating an Executable Console Application in PHP

There are a few tools out there that allow you to create an executable console application in PHP, but they tend to be a little heavy and limited to a certain OS. With PeachPie, console apps can be created straightforwardly, without having to distribute the sources, and it can be debugged right in Visual Studio.


Before we get into it, let’s make our lives significantly easier. If you’re completely new to PeachPie and this is your first time hearing about it, it’s a compiler and runtime of PHP under .NET & .NET Core. Therefore, it allows PHP developers to take advantage of the full toolset available to the .NET world effortlessly out of the box. One of those tools is Visual Studio, so let’s go ahead and grab that first:

Install Visual Studio 2019 or newer

Now that we have Visual Studio, download the PeachPie for Visual Studio extension, which does much of the dirty work for you.

Creating a project

The first step is to create a project. With PeachPie for Visual Studio, we use the standardized build system for .NET – MSBuild. Instead of configuring any of this yourself, use the PeachPie extension to create your project. Either use the Create a new project option on Visual Studio 2019’s start screen or navigate to File | New | Project (or use the Ctrl+Shift+N shortcut).

Now select the PHP language and run a search for “PeachPie” in the project templates, which will populate a list of various templates for you:

New project wizard in our Visual Studio extension

In the window, select thePeachPie Console App (.NET Core) template, and then continue by hitting Next. If you can’t see the project template, make sure you have our PeachPie for Visual Studio extension installed.

Now you can pick the project name choose and its location. Continue by pressing the Create button.

New Project Wizard

Building and Debugging

Not only can you create a binary file from your PHP code, you can also run and debug it. PeachPie compiles PHP code to an executable running under the .NET Core runtime, which means you can execute it on Windows, Linux, MacOS and whatever other system .NET Core supports. Let’s go ahead and build the app:

Hit Build, in the project menu and keep an eye on the Output pane in Visual Studio, which provides a log of the build process and reveals exceptions or issues.

To debug your app, either hit the green arrow (Start) or press F5 to build and debug the application:

If everything goes smoothly and your console app doesn’t contain any errors, you’ll get a window that looks something like this:

Hello World! 
ConsoleApp2.exe (process 14116) exited with code 0. 
Press any key to close this window . . . 

Setting a startup object

You now have a simple hello world application, consisting of a single script file. However, the application will need to know where to run from once there will be more logic and more code. By default, using this project template, the application starts with the program.php script file. This is a build property of the application, and you can only have a single startup script specified.

The startup object is a build property, so you can specify just a single startup object for the project. However, you can define more build configurations, each with different properties.

Now let’s create a function, main(), and set it as the startup object instead of program.php. Replace the code in program.php with the following:


/** The entry point of the program. */
function main()
    echo "Hello World!";

However, if you were to start the application now, nothing would happen. Nothing is calling the new function and thus the program simply exits. To control what runs after the application starts, we have to change the build property StartupObject. Edit the the projecty file by right clicking on the project in the Solution Explorer and choose Edit Project File.

New Project Wizard

Navigate to the line


and change it to


You can specify can specify any PHP file relative to the project root, or a function here.

Running the program again

Now we can try it again. Place a breakpoint (F9) in there, as depicted in the image below:

and start debugging the programm (F5). The app will now re-compile and start while being debugged.

So what now?

You’ve created a very basic console app in PHP. It shouldn’t stop there! Firstly, you can gradually start adding more and more code and creating a more robust console app. Another use case is to create a self-contained executable, as described in this article or our docs.

More importantly, however, you can integrate such an application with a C# project or NuGet packages.

Posted on March 15, 2020, in category Tutorial, tags: , , , ,