Computer lessons

Free PHP compilers. A selection of online compilers: run and test code directly in the browser What is a PHP compiler

Compiling PHP from source code is often done on Unix-like systems. Those who work in a Windows OS environment will most likely download and install PHP from binary packages. And while I don't agree that it's easier to use a precompiled solution, even on Unix systems there are some benefits that can come with compiling a binary from source. All in all:

  • You have the ability to fine-tune the final product during compilation. Maybe you want a certain extension that compiles directly to the binary rather than loading it as an external library. Or perhaps you want to turn off something that is usually available by default.
  • You can do a trick, during the compilation process if necessary, that may improve performance for a specific environment (of course, this assumes you already know what you're doing in this case you wouldn't read this article !).
  • Compiling may be the only way to get things working if the compiled binaries were built on older versions of supporting software and libraries, and you are now running on a new system.

Warning: compilation can be frustrating, especially on Windows! You must ensure the build environment is set up correctly, learn how to use the compiler and other build tools properly, and satisfy any library dependencies. We hope this article is your first step in overcoming many of these obstacles.

Setting up the build environment

PHP is written in C and therefore a C compiler is required if you are going to build PHP from source code. C++ is a super suite of C, so a good C++ compiler should be able to compile C code, and although this is not always the case. For Windows, Visual Microsoft, C + + Express (which will later be called VC + +) is freely available on the Microsoft website. The 2010 edition was used.

When choosing a compiler version, you should keep in mind how you will run PHP. If you have to work with mod_php of the officially compiled Apache binaries, and you want to, compile PHP using Visual Studio 6, since this is the Apache compilation version. The module must target the same runtime library as Apache, in this case msvcrt.dll. If you are building Apache from source as well, or if you are going to run PHP as FastCGI or CLI, then this is not a problem and 2010 will work fine.

You must also install the Windows Development Kit (SDK) software. The SDK gives us the important header files for the Windows platform that we will need to compile successfully. This too, version 7.1 was used.

Install the compiler and then the SDK. I won't discuss installation as both have a graphical installation wizard that will guide you through the entire process.

If you have a working compiler build, download the Binary Tools and Known Packages from windows.php.net. The binary tools package (I'm using the 20110915 archive) contains development tools like re2c, bison, and some additional commands that you will need to build PHP. The known package (I'm using the 5.4 archive since it matches the PHP version I'll be compiling) contains the minimal headers and dependency libraries needed, eg zlib.h.

It probably goes without saying that you want to download the PHP source as well from windows.php.net. As of this writing, the current version of PHP is 5.4.6, so that's the version number you'll see in the examples.

It's a good idea to create a workspace to which you can extract the source code and compile it so that it doesn't affect the rest of your system. Create a folder C:\PHP-Dev that will serve as your working directory, and then extract the binary archive and tools into it.

Next, extract the contents of the archive, PHP source in C:\PHP-Dev now you have php5.4 in the source folder and then extract its deps archive into the deps sibling folder. The directory structure should look something like this:

Open the Windows SDK Command Prompt that was installed with the SDK (Start => Microsoft Windows SDK => Windows SDK Command Prompt) and run the following commands:

Setenv /release /xp /x86 cd C:\PHP-Dev bin\phpsdk_setvars.bat

Using the SDK console command line is preferable before the regular cmd.exe console, as it sets many environment variables specific to source code compilation. Compilations of commands later must also be done in this console.

setenv sets some build properties for the environment, in this case the target environment is Windows XP 32-bit version of the build. You can try and build with /x64 if you're looking for adventure. Defining different versions of Windows such as /Vista will most likely have output problems due to some weird definitions in the scripts (PHP still wants to be XP compatible). Unless you really know what you're doing, it's probably safest to stick with the recommended values ​​I used above.

The phpsdk_setvars.bat script accesses some additional environment variables, the build process was able to find the binary tools.

Keep in mind that all these variable settings are only temporary sessions in the console. If you quickly close everything to come back to compile later, you will need to run the command again, and if you don't, you will get errors like the following, when you later run configure, you will not be able to continue:

Checking for bison.exe ... ERROR: bison is required

Making sure you have the right build environment, the necessary sources, and any dependencies is the hardest part of the process. So now your environment is set up with source code and dependencies in place, it's time to compile!

Compiling PHP

At the SDK command line, go to the PHP source folder and run buildconf. The command is responsible for generating configuration files that will be created by the Makefile to control the compilation process.

After buildconf finishes (this will only take a second), run configure --help - and examine the help for what features you want to enable/disable, then run configure again with whatever option you want. It's a good idea to check the output before moving, as it will warn you if any of the required dependencies are not available. If this happens, you can either install the dependencies and re-run the setup again, or adjust the calls to disable extensions that need them.

Finally, run NMAKE to start compiling.

Cd C:\PHP-Dev\php5.4 buildconf configure nmake nmake test

If any configuration or NMAKE fails, the problem is one of two: First, the environment is not configured correctly, second, you have enabled a feature that depends on external libraries and the libraries are not installed on your system. Double check that you have created the environment according to the instructions above, and that any additional libraries that may be required in the underlying configuration settings have been configured.

When the first NMAKE of the compilation process is complete you will find your brand new PHP files in the Release_TS folder. The NMAKE test runs new double capacity error tests to ensure everything is working as it should. NMAKE test results are sent to the QA team who depend on them to improve PHP, so it may take a few minutes to work on, it's a big deal.

At this point you can also take advantage of the additional step of running the NMAKE snap-in, which will create ZIP archives and binaries that you can copy around.

Compilation extensions

There are two ways to compile PHP extensions: statically and dynamically. A statically compiled extension is compiled into a PHP binary, while a dynamically compiled extension is one separate DLL that can be loaded later via a php.ini file. Extensions are typically compiled as DLLs, although there are some advantages to static compilation, and ultimately it depends on your needs.

To compile PHP extensions on Windows, extract the source code extension folder into the ext folder of your PHP source directory. Then, reconfigure the script by running buildconf --force and recompiling PHP using the appropriate options to enable the extension.

As an example, let's compile the AOP extension statically. Download the source code from PECL, and extract it into a folder, ext. Then follow these steps:

Cd C:\PHP-Dev\php5.4 buildconf --force configure --enable-aop nmake

The --force, buildconf option forces it to restore the configuration script. Next, run configure --help and you should see the option to enable the new extension in the output. In this case, it is --enable-AOP.

When nmake finishes, you will have a newly built PHP binary with AOP.

The extensions will be available as a DLL rather than baked in PHP, you can follow the same steps as above but specifying "shared" as the value for the configuration allows the option.

Buildconf --force configure --enable-aop=shared

As a result, the DLL will be in the Release_TS folder along with the PHP binary when compilation ends, in this case the name is php_aop.dll .

P.S.

Compiling on Windows is still a bit tricky, especially when it comes to extensions. Being able to compile source code is a good skill, especially if you want to change PHP later.

The article was prepared for you by the site team
Original article:
Translated by: Victor Klim

Almost all developers sooner or later face the need to run or quickly check some code, but not everyone knows that for such a simple task it is not at all necessary to run heavy desktop IDEs or application compilers. It is enough to use online tools that allow you to do everything much faster: Ctrl+C, Ctrl+V, Run, whack - and the output of the program is already before your reddish eyes.

We have selected the best online compilers: some of them are quite universal, others are tailored for strictly defined tasks. In any case, they will not be superfluous.

Koding

Koding.com is not an online compiler in the traditional sense. Each user of the service can create several full-fledged virtual machines running Ubuntu 14.04 in the cloud, on which they can do whatever they want, including compiling code. All popular languages ​​are supported by default, but you can easily add your own.

In addition to the control panel for your server, a convenient IDE and a terminal window are available in the interface. Koding is the most universal tool; next we will look at simpler and more specialized options.

IdeaOne

IdeOne is an online compiler and debugging tool that allows you to run code in over 60 programming languages ​​and their specific versions directly in the browser.

For those who do not have a girlfriend, the creators have provided code compilation in the Brainfuck language.

JDoodle

Another online compiler that supports many languages, including some that you won't find in many other online compilers. A nice feature of JDoodle is the ability to collaborate - just send a link to your current session and generate bugs at double speed!

jsFiddle

Don't let the name fool you - jsFiddle isn't just built for JavaScript. This online front-end editor allows you to test any combination of JavaScript, HTML and CSS. Of course, there is support for various frameworks, for example, jQuery, Vue, React, TypeScript, as well as CSS preprocessors like SCSS. For convenience, you can select a key binding from your favorite editor. True, only if your favorite editor is Vim, Emacs or Sublime Text.

CodePad

CodePad is a minimalistic service in which you can store code, share it, and run it with subsequent output of the results of its execution. There are several of the most common languages ​​to choose from, but, unfortunately, no choice of specific versions of interpreters or compilers.

Its main advantage is its simplicity and ease: the site will work quickly even with a slow Internet connection. Auto-connection of standard headers is provided, as well as integration with Vim or Emacs.

One of the disadvantages is the complete lack of syntax highlighting when entering code into the form. However, when viewing an already saved recording, the backlight is present.

GCC GodBolt

GCC GodBolt is an interactive C++ compiler. I got into this collection for the reason that it has a simple interface, as well as a large number of settings, including options that can be adjusted using keys.

There are many compiler versions to choose from, including the latest ones. One of the interesting features is the instant translation of the program code into assembly language.

PHP is an interpreted programming language; with each request, the source code is analyzed and “executed”. This approach, of course, is very convenient at the project development stage, but it introduces an extra step into the process of executing production code. Thus, interpretation, which at first glance is PHP's strong point, costs extra CPU time and resources.

Below we will talk about compilers that allow you to compile PHP code into C++, and it into executable code. Thus, PHP applications are executed directly by the processor, bypassing the interpreter.

Let's check if everything is so good in practice.

How the interpreter works

Interpretation of PHP code takes place in two stages:

  1. Code parsing and generation of opcodes (Zend opcodes) - instructions understandable to the interpreter.
  2. Execution of opcodes.

While the first phase lends itself well to optimization (using an opcode cache), the second is quite closed - the interpreter is always an intermediary between the set of instructions and the processor executing them. Without an interpreter, the processor cannot understand what to do with opcodes.

To get rid of the interpreter link, compilers were invented, the most popular and recent of which is HipHop from Facebook. Let's feel it closer.

HipHop PHP

HipHop is written by Facebook developers and is an application that:
  1. optimizes PHP code
  2. converts to C++
  3. generates from your application a multi-threaded web server that executes it
  4. compiles into executable code using g++

Thus, the input is PHP code, the output is a server, part of which is the written functionality.

Let's check how HipHop can handle compiling an application written using a framework, such as Wordpress.

Compiling Wordpress

After installing HipHop, in the src/hphp/ folder we will get the hphp file, which is the compiler. Before compilation starts, set the environment variables:

Cd .. # go to the folder with hiphop export HPHP_HOME=`pwd` export HPHP_LIB=`pwd`/bin export CMAKE_PREFIX_PATH=`/bin/pwd`/../

and go ahead!

Download Wordpress and unzip the archive:

Wget http://wordpress.org/latest.tar.gz tar zxvf latest.tar.gz

Copy wp-config-sample.php to wp-config.php and specify the settings for connecting to the database (in the host settings we specify 127.0.0.1, not localhost).

For successful compilation you need to patch Wordpress a little:

  1. Open wp-includes/js/tinymce/plugins/spellchecker/classes/SpellChecker.php and replace: function &loopback(/* args.. */) ( return func_get_args(); ) with function &loopback(/* args.. */ ) ( $ret = func_get_args(); return $ret; )
  2. In wp-includes/query.php, instead of if (!isset($q["suppress_filters"])) $q["suppress_filters"] = false; insert $q["suppress_filters"] = true;

Wordpress is ready.

Hiphop needs to specify the list of files that we will compile - we will get it and save it in files.list:

Find. -name "*.php" > files.list

Everything is ready for compilation, let's proceed:

$HPHP_HOME/src/hphp/hphp --input-list=files.list -k 1 --log=3 --force=1 --cluster-count=50

After completing the command, in the temporary folder (at the beginning of compilation, hphp will show its path, something like “/tmp/hphp_ptRgV1”) we will receive a compiled web server. Let's launch it (if something is hanging on port 80, for example apache or nginx, you must first stop it to free the port):

Sudo /tmp/hphp_6s0pzd/program -m server -v "Server.SourceRoot=`pwd`" -v "Server.DefaultDocument=index.php" -c $HPHP_HOME/bin/mime.hdf

Voila! By going to http://localost we will see a working Wordpress blog.

Performance

Let's see if there will be a performance increase compared to the uncompiled version of WordPress running on apache2. Below are graphs of the dependence of page generation speed on the number of parallel users.

As you can see, the results were shocking: the compiled blog runs on average 6 times faster! The average number of requests processed per second in the uncompiled version is 9, and in the compiled version it is 50! I don’t know about you, but these results amazed me; I didn’t expect such a strong performance increase.

Summarize

After such stunning results, only one thing can be said - the guys from Facebook did a great job. The compiler really makes a rocket out of an application, and although the application needs to be prepared before compiling, the result is worth it.

To the point:

If you liked the post, click on Google +1 - it will give me more motivation to write and it will just be a pleasure.

All free PHP compilers that are presented here can rebuild PHP scripts into machine code that can run on a computer without downloading a special PHP interpreter, or compile them into a bytecode command line interface (NET or Mono framework or Java bytecode is required for installation (where a Java Virtual Machine is required for installation)).

Such compilers can be useful for a variety of purposes: they can speed up the execution of your script because they are no longer interpreted at runtime; or thanks to them, you can distribute your applications without revealing the source code (which other commercial scripts require). I suppose they are also suitable for the case where someone wants to write web-dependent PHP programs and distribute them with the functionality of running on the desktop (as opposed to regular web applications that run on a server), this is all possible because that PHP is an easy-to-learn programming language and basically contains many built-in functions with Internet access. (In this case, you will either have to distribute applications with a built-in web server or use a compiler that compiles the server into your application.)

By the way, if you want to use obfuscation in your code, then know that this is also possible when using PHP accelerator. Such accelerators also imply an increase in the speed of execution of your script.

Useful information for those who do not yet know that the official version of the PHP translator can be downloaded from the PHP website: Hypertext Processor.

Free PHP compilers for composing native code, .NET or Java bytecode scripts.

Bambalam (new)

This program produces standalone Windows EXE applications for your PHP source code. It's not really a native code compiler since it just encodes source code and embeds a PHP interpreter, but it's certainly a good fit for people looking for native and byte-code compilers. By the time the entire program was written, its execution environment was the built-in version of PHP 4.4.4 (the program had not been updated since 2006). The source code for Bambalam is publicly available.

Phalanger (for .NET)

Phalanger compiles your PHP code into CLI bytecode (.exe or .dll). This program can be run through .NET 4.0 or Mono frameworks. Your PHP code can use any .NET objects and additional standard PHP extension libraries. The resulting NET assembly can be either signed or hidden. The program also produces templates that allow you to create PHP applications using Visual Studio. The program is released under the Apache license.

HipHop for PHP (for native code)

HipHop translates your PHP code into C++ code, which is later compiled using the GNU C++ compiler into executable binary code. The compiler supports all the features of PHP 5.3 (except of course for things like eval()). It works and compiles code for 64-bit Linux and FreeBSD. Since the program is distributed in source code form, you will have to compile manually (yourself). It is released under the PHP License.

Roadsend PHP (for native code).

The Roadsend PHP compiler produces native binaries (executables) for Windows and Linux. Your scripts are not limited to console programs (command lines): they can be built using embedded web servers, allowing them to run in the same way they run on a website, albeit on your own user system. The compiler is released under the GNU GPL license and runs under the GNU LGPL. Unfortunately, the program has stopped its active development.

Project Zero (for Java)

(Note: This software appears to be defunct. The site has been inaccessible for about half a year now.) Project Zero includes a compiler and CLR that can compile your PHP code into Java bytecode and run it. Note that Project Zero is more than just a PHP compiler/runtime; it provides a rich framework that allows you to enhance web applications using PHP or Groovy (another scripting language). This compiler is available for Windows, Mac OS X and Linux. In order to work with this compiler, you will need to download the Java Development Kit.

Which of the proposed compilers do you prefer? Or do you have another favorite translator? Leave your remarks and comments below, we will be happy to read them and reel them in.

Tags: PHP compilers, script translation