A selection of online compilers: we run and test the code directly in the browser.

PHP is an interpreted programming language, each request is parsed and "executed" source code. 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 the interpretation, at first sight forte PHP costs extra CPU time and resources.

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

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

How the interpreter works

The 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 the opcode cache), the second is rather closed - the interpreter is always an intermediary between the instruction set and the processor that executes them. Without an interpreter, the processor can't figure out what to do with opcodes.

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

hiphop php

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

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

Let's see how HipHop handles compiling an application written using a framework like Wordpress.

Compiling Wordpress

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

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

and go!

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 database connection settings (specify 127.0.0.1 in host settings, 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"you need to specify a 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 start:

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

After the completion of the command, in the temporary folder (at the beginning of the compilation, hphp will show its path, something like "/tmp/hphp_ptRgV1") we will get the compiled web server. Let's run it (if something hangs 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! Going to http://localost will see a working Wordpress blog.

Performance

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

As you can see, the results are shocking: the compiled blog works on average 6 times faster! The average number of requests processed per second in the uncompiled is 9, and in the compiled 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 the 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 - I will be more motivated to write more and just nice.

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, whoop - and the output of the program is already in front of 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 redundant.

coding

Koding.com is not an online compiler in the usual sense. Each user of the service can create several full-fledged virtual machines in the cloud under running Ubuntu 14.04, on which he can do whatever he wants, including compiling the 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. coding is the most universal remedy, then we will consider simpler and more specialized options.

IdeOne

IdeOne is an online compiler and debugging tool that allows you to execute code in more than 60 programming languages ​​and their specific versions right in the browser.

For those who do not have a girlfriend, the creators have provided a compilation of code 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 spawn bugs at double the speed!

jsFiddle

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

codepad

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

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

Of the minuses, one can name the complete lack of syntax highlighting when entering code into a form. However, when viewing an already saved record, the backlight is present.

GCC God Bolt

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

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

Compiling PHP from source is more 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 pre-compiled solution, even on Unix systems There are some advantages that can come with compiling binary from source. All in all:

  • You have a chance fine tuning final product when compiling. Maybe you want a certain extension that compiles directly to binary instead of loading it as an external library. Or perhaps you want to turn off something that is a feature that is usually available by default.
  • You can do tricks during compilation if needed that can improve performance for your particular environment (of course, this assumes you already know what you're doing in this case. you wouldn't read this article !).
  • Compilation can be the only way make things work if the compiled binaries were built on older versions with software and library support, and now you're running on a new system.

Warning: compilation can be frustrating, especially on Windows! You must ensure that the build environment is set up correctly, learn how to use the compiler and other build tools properly, and satisfy any dependency libraries. 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. C++ is a superset of C, so a good C++ compiler should be able to compile C code, and although this isn't always the case. For Windows, Visual Microsoft, C + + Express (which will be called VC + + after) 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 be running PHP. If you have to work with mod_php the officially compiled apache binaries and you want to compile php using visual studio 6, as 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 just fine.

You must also install the software Windows software Development Kit (SDK after). The SDK gives us important files headlines for Windows platforms, which we need for successful compilation. 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.

Once 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, which you will need to build PHP. The known package (I'm using the 5.4 archive because it matches the version of PHP I'll be compiling) contains the minimum headers and library dependencies needed, such as zlib.h .

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

it a good idea to create a workspace to which you can extract the source code and compile it without affecting the rest of your system. Create a folder C:\PHP-Dev to serve as a 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 to 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 command line SDK console is desirable before the normal cmd.exe console as it sets many environment variables specific to source code compilation. Command compilations later must also be done in this console.

setenv set some properties of the assembly for the environment, in this case the environment of the target Windows XP 32-bit version of the assembly is set. You can try and build with /x64 if you are looking for adventure. Definition of various Windows versions, such as /Vista , most likely exit problems due to some strange definitions in 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've used above.

phpsdk_setvars.bat script goes for some extra environment variables, the build process was able to find the binary tools.

Keep in mind that all of these setting variables are only temporary sessions in the console. If you quickly close everything to return to compilation later, you will need to run the command again, and if you do not, you will receive 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 correct build environment, the required sources, and no dependencies is the hardest part of the process. So now that your environment is set up from source and dependencies in place, it's time to compile!

Compiling PHP

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

After buildconf completes (it only takes a second), run configure --help - and examine the help for which features you want to enable/disable, then run configure (settings) again with whatever option you want. It's a good idea to check the output before going, 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 call to disable extensions that need them.

Finally, run NMAKE to start compiling.

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

If either configuration or NMAKE fails, the problem is one of two: First, the environment is not set up 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 as instructed above, and that any additional libraries that may be required in the base configuration settings have been configured.

When the first NMAKE compilation process is complete you will find your brand new PHP files in the Release_TS folder. The NMAKE test runs new double through capacitance error tests to make sure everything works as it should. The results of the NMAKE tests are sent to the QA team, which depends on them to improve PHP, so it may take a few minutes to work, this is a responsible matter.

At this point, you can also use the optional NMAKE snap-in step, 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 one is a separate DLL that can be loaded later via the php.ini file. Extensions are usually compiled from the state of the DLL, although there are some benefits to static compilation as well, ultimately it depends on your needs.

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

As an example, let's compile an AOP extension statically. Download the source code from PECL , and extract it to a folder in ext . Then do the following:

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

With the --force option, buildconf forces it to restore the configuration script. Then, run configure --help and you should see an option to include the new extension in the output. In this case, it's --enable-AOP.

When nmake finishes finishes, you will need 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" (shared) as the value to configure allows the option.

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

The resulting DLL will be in the Release_TS folder along with the binary PHP compilation will end, in this case the name is php_aop.dll .

P.S.

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

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

All free compilers PHP, which is presented here, can be recompiled 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 (the installation requires NET or Mono framework or Java bytecode (where required virtual machine Java to install)).

Such compilers can be useful for a variety of purposes: they can make your script run faster 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 assume they are also suitable in the case where one wants to write internet dependent PHP programs and distribute them with a desktop run feature (as opposed to regular web applications which 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 access to the Internet. (In this case, you will either have to distribute applications with an embedded 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 you should know that this is also possible using PHP accelerator. These accelerators are also supposed to speed up your script execution.

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

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

Bambalam (new)

This program produces independent Windows Applications EXE for your PHP source code. It's not exactly a native code compiler as it just encodes the source code and embeds a PHP interpreter, but this program is definitely for people who are 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 has not been updated since 2006). The source code for Bambalam is in the public domain.

Phalanger (for .NET)

Phalanger compiles your PHP code to 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 libraries of the standard PHP extension. The resulting NET assembly can be either signed or hidden. This 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 features of PHP 5.3 (except, of course, for such a thing as 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 it 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 with built-in web servers, allowing them to work the way they work on a website, albeit on your own user system of course. The compiler is released under the GNU GPL and runs under the GNU LGPL. Unfortunately, the program stopped its active development.

Project Zero (for Java)

(Note: this appears to be software is now non-existent. The site has been out of reach for half a year now.) Project Zero includes a compiler and CLR that can compile your PHP code into Java bytecode and execute it. Note that Project Zero is more than just a PHP compiler/runtime; it is a mature 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 compiler do you prefer? Or do you have another favorite translator? Leave your remarks and comments below, we will gladly read them and wind them up.

Tags: PHP compilers, script translation

Programming languages ​​come in two varieties: interpreted and compiled. What language is PHP? In order to answer this question, we need to understand the terminology.

A program that translates code written in one programming language into another is called a translator. A compiler is also a translator. It translates code written in the language high level, to machine code. The compilation process produces a binary executable file, which can already be run without a compiler.

The interpreter is a completely different category. The interpreter does not translate the code, but executes it. The interpreter analyzes the program code and executes each of its lines. Every time you execute such code, you must use an interpreter.

In terms of performance, interpreters are significantly inferior to compilers, since binary code executes much faster. But interpreters allow you to fully control the program during its execution.

As far as PHP is concerned, it is neither a compiler nor an interpreter. PHP is a cross between a compiler and an interpreter. Let's try to understand this and consider how PHP processes the code.

Consider the figure:

We see that PHP is made up of two almost independent blocks - a translator and an interpreter. Why was it necessary to do so? Of course, for speed reasons.

The PHP input is a script. It translates it (translates), checking the syntax, into a special bytecode (internal representation). PHP then executes the bytecode (not the actual program code) without creating an executable.

Bytecode is much more compact than ordinary program code, so it is easier (and faster) to interpret (execute). Judge for yourself: parsing is carried out only once at the stage of translation, and the "semi-finished product" is already being executed - the bytecode, which is much more convenient for these purposes. Therefore, PHP is more of an interpreter than a compiler. Such "double work" was necessary for the following purposes.

Consider a loop:

For(i=0;i<10; i++) { Operator_1; Operator_2; Operator_3; ............ Operator_99; Operator_100; }

Such a cycle will "spin" 10 times. For each of these ten passes, the interpreter must and 100 lines of code. And in it you need to analyze and execute 10 * 100 = 1000 lines of code! If you translate the entire cycle once into bytecode, then it will have to analyze 10 times less! This means that scripts will run 10 times faster!

It turns out that PHP is.

The main phase of PHP's work is the interpretation of the internal representation of the program and its execution. It is this phase that takes the most time in serious scenarios. However, the slowdown is not so significant.

It's worth remembering that PHP version 3 was a "pure" interpreter, and with PHP 4, scripts run much faster, since PHP 4 (and PHP5) is an interpreter.

The Perl language, which is almost always called a compiler, works in exactly the same way - it translates the program text into an internal representation, and then uses the resulting code when it is executed. So you could say PHP version 4 is a compiler just as much as Perl is.

So, we are forced to conclude that PHP is an interpreter with a built-in translation unit that optimizes the course of interpretation.

Using an interpreter (and therefore PHP) has its undeniable advantages:

  • There is no need to worry about freeing the allocated memory, there is no need to close the files when you finish working with them - the interpreter will do all the routine work, since the program is executed under its watchful control;
  • You don't have to think about variable types, and you don't have to declare a variable before it's first used;
  • Debugging programs and detecting errors are greatly simplified - the interpreter has full control over this process;
  • In the context of web applications, the interpreter also has another very important advantage - there is no danger of the server "hanging" if the program does not work correctly.

There are other benefits as well. In general, using an interpreter can give scripts the power that Web users expect from them.

PHP's performance penalty is noticeable in the case of large and complex loops, when processing a large number of lines, etc. However, note that this is the only drawback of PHP, which will become less and less manifested as more powerful processors are released, so that, in the end , to disappear altogether.

<<< Назад
(What's new in PHP5?)
Content Forward >>>
(Moving to PHP 5.3)

If you have more questions or something is not clear - welcome to our
A computer