A selection of online compilers: we run and test the code directly in the browser. Free PHP Compilers PHC Example

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 options for adjustable 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.

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.

Alexey Romanenko: My name is Alexei Romanenko, I work for RBC. The topic of this report is somewhat controversial. It would seem, why compile PHP scripts when everything seems to work anyway?

Probably the main question: "Why?" In general, the purpose of this presentation is to try to understand whether such a compilation is needed, if so, why, in what form and to whom.

What is a PHP compiler?

First, a little overview of what a PHP compiler is. I'll tell you how it works, what it is and how you can speed it up.

The first functional module is the so-called SAPI (Server API), which provides an interface for accessing PHP from various clients (Apache, some kind of CGI server (Common Gateway Interface) and others). There is also SAPI embedded, which allows you to embed PHP into any application.

The second main part is PHP Core, which handles requests, implements all networking, file system and parsing the scripts themselves.

The third global part is the Zend Engine, which compiles our scripts into some bytecode, executes it on its virtual machine and manages memory (implements comprehensive allocators).

One of the most important and big parts is the Extensions module, which implements 99% of what we use in PHP. These are either "wrappers" for some libraries, or functionality, or classes, built-in libraries, and so on. We can also write our own extensions.

How is the script itself executed?

First. Lexical analysis takes place - a file is loaded, parsed, all characters from the set of this file are translated into a certain set of tokens, with which we then work.

The parsing phase parses these tokens. Based on this analysis, a certain grammatical structure is compiled, on the basis of which the bytecode will then be generated.

At the end, Zend Engine executes it. The result is returned to the client.

We are talking about high loads. If you repeat this scheme of actions with them every time, everything will work very slowly. When several hundreds or thousands of requests come to our interpreter at the same time, there is simply no speed.

But there are solutions. They have been known for a long time.

How to achieve acceleration?

The simplest, cheapest, and well-tested solution is bytecode caching. Instead of going through parsing, parsing phases, we just cache our bytecode. There are special extensions for this - they are well known to everyone who has worked with PHP - these are APC, eAccelerator, Xcache and so on. Zend Engine simply executes the bytecode.

The second option is code profiling, identifying bottlenecks. We can rewrite something as PHP extensions (it will be a C/C++ extension) compile it and use it as modules.

The third option is more global - forget about PHP and rewrite everything. In general, the option has the right to life, but only in the case when there is not enough php code. In large, large projects (or which exist for quite a few for a long time) it usually accumulates a lot, and it will take a long time to rewrite everything. The business requirements won't let you do that. In general, writing something in PHP, for example, for a front-end server is not too long, because it is a simple language. It allows you to quickly do things that take longer to do in low-level languages.

There is also an alternative option that has recently become widespread - this is to compile PHP somewhere, into something faster.

Let's compile something, shall we?

By the word "compilation" I will mean the translation of the PHP script code into something else, into some other code.

In this case, it can be of two types.

Native code is a kind of binary file that can be executed on a physical machine.

Non-native code. You can compile some bytecode that can be executed on another virtual machine, for example, on the JVM.

What can be used to compile native code from PHP?

Roadsend Compiler. Its sequel is Raven. There is also PHC (this is a PHP Open Source compiler). Recently, HipHop (Facebook) has also appeared.

I will give a small overview of what can be done for non-native code. As far as I know there are 3 working options. These are bytecode generation for Java and bytecode generation for .Net: Quercus, Project Zero, Phalanger. I will not consider compilation to non-native code, because we do not use it. Let's get back to compiling to native code.

In my opinion, the oldest compiler is Roadsend. It began to be developed quite a long time ago, in 2002. It was originally a commercial application. It was closed, only in 2007 it was released in Open Source. There is a very complex compilation scheme: a certain Bigloo compiler for the Scheme language is used, after which native code is generated. This compiler does not use the Zend Engine.

We can either generate a standalone executable binary or generate a module for Apache. It is also possible to generate a binary that will act as a web server. But it doesn't work. I don't know why, but it doesn't work for me at all.

Work on Roadsend, as far as I know, is not currently underway. It was reborn as the Raven project, which was completely rewritten in C++. As a compilation, it uses LLVM to generate code.

On this moment everything looks very promising.

But it is still under construction. Even in the documentation there are hints that we will not generate binaries. Wait.

Everything would be sad if we didn't have PHC. This is an open source compiler. It has been developed since 2005. One of its cons: it uses built-in SAPI. We do not abandon the Java machine, the Zend Engine. Essentially, it translates PHP code into PHP extension module code. After that, it compiles, but the execution process, again, uses the Zend Engine.

PHC example

Very similar to how we work, for example, with conventional compilers, gcc. The first one shows that there is one binary, we can also just generate the C code. Since the same gcc is used internally after generating this code, we can use those flags that are intended for optimization and other things.

It was about an application that runs on the command line.

To launch a web application, you need to perform several actions, it is quite difficult. First you need to generate an extension (eng. Extention), then compile the code, and then somehow (either dynamically or statically) connect it.

Key Benefits of PHC

In fact, we use the same PHP, we have full compatibility. All other extensions are supported. Everything that we have compiled, we use. Pretty good documentation.

By the way, one of the added bonuses of PHC is that we can generate the XML work of our script based on how the XML is built, sometimes this can be useful.

Minuses

In my opinion, this is an inferior binary, because it still has a dependency on the Zend Engine. There is also some difficulty in terms of connecting web projects.

The main thing

Probably, this report would not have happened if HipHop, a solution from Facebook, had not appeared. Its creators also accumulated a large amount of PHP code and thought for a long time what to do with it.

As I understand it, after the rewrite options were rejected, it was decided to write a translator (in this case, into C++ code). The project is relatively young, officially it was released only in February of this year. PHP code is translated into C++ code and then generated standard means your operating system. True, while only supported operating system linux.

Just yesterday, I asked a Facebook representative about this decision. He said that at the moment 100% of the PHP code is compiled via HipHop. In its pure form, the code does not work through the PHP interpreter. Again, the creators claimed a significant reduction in processor load.

The main functionality of HipHop

It directly generates the binary itself, which can be executed on the command line. There is such an option to launch it as a streaming web server. There is also a separate built-in debugger. It can debug scripts both locally and remotely (it will work as a server).

The assembly process is rather non-trivial. There is a description, but it is not collected everywhere. At the moment, as I said, everything is going under Linux, plus initially everything was "sharpened" for 64 bits. Although now 32 bits are experimentally supported. But I managed to compile and patch a little - in general, he did all this, because by default it is not collected.

They also have their own versions of libcore and I think there are a couple of libraries that need to be patched too. In general, the assembly process is not so simple.

At the output after assembly, we get a certain hphp file, which is a translator of our PHP code into C ++. If we look at the flags, there are quite a few of them. I have highlighted here a few of the main ones that you may need.

As a configuration file (config) we can use a file in HDF format by setting various directives. We can set the level of errors and other things there (HDF is all that is available in a structured form). We can also take the config itself from the database or use it directly on the command line.

We set the logging level during compilation: show all errors or also display warnings (English warnings), Additional information, or generally keep a full log of everything that happens.

A very useful directive is input_list=FILE which allows us to specify a list of scripts we want to compile. It is also worth mentioning such a directive as lazy-bind. We can specify all project files - those that will be compiled.

PHP script compilation example

The third level of logging has been set, here is pretty general information on time, you can see how much it took. Actually, the script is quite simple. This is the usual "Hello, World", I just took a screenshot.

The "heaviest" file is our "program" binary, its size is 28 MB. In fact, our "Hello, World" is 28 MB. I wanted to note that in addition to the standard line "Echo "Hello, World!", this binary includes a lot of other things. This is a full-fledged web server, a full-fledged server for administration.

What do we get?

We have "Hello…" in C++, which executes a function consisting of one line "echo "Hello, World". In addition, a lot of third-party things are loaded. As we can see, this is a full-fledged C++ file.

This is the resulting program. It already has quite a few different keys for different configurations, but I will note only a few of them.

This is --mode, this is the launch mode of our program. We can run it both directly (from the command line) and as a web server or a full-fledged daemon. There are a couple more options, but they are not essential.

Used config in the same format. I did not begin to give directives, because there are a lot of them. HipHop comes with documentation. It is not on the wiki site, but documentation is provided with the distribution kit, where everything is clearly described. I did not even expect that the description would be so detailed. This allows for quite flexible configuration of the solution.

To run in server mode, we can specify a port. For administration, a separate port is used, where you can add some requests that allow you to manage the server. If we have a debug server running, then we specify the host and port where we will "bind" for debugging.

Launch example

For example, we specified port 9999 for broadcasting. By performing simple http requests, we can either get statistics, or somehow control the server, or get some additional information. In general, it is very convenient.

Option to get status information

It is possible to get the status-set of the server, in various built-in formats (xml, json, html). In fact, information is provided about the server master process itself and handlers - threads that process requests.

Additional statistics

In fact, a lot of statistics are provided. Since HipHop natively works with memcache and SQL in the form of MySQL, it provides detailed information for all operations that are performed with it.

Full memory statistics

Here is a very useful feature - Application Stats. Using the additional functions of HipHop itself in PHP, we can write statistics in our scripts, which we then receive through normal http access.

Debugging

As I said, it is possible to use the built-in "debug" to debug scripts. This is very convenient, because the hphpi interpreter works similarly to what we have compiled. There is a difference in the "behavior" of scripts when they are executed in standard PHP and when using some data from the compiled ones. To debug what is compiled, Facebook has written a separate interpreter.

In the first case, we run the code with the “-f” key and see how the file behaves; all output goes to stdout. Or we can run it in debug mode and get into the interactive debugger. It is very similar to standard GDB: you can also set breakpoints (eng. breakpoints), run, enter variable values, tracking, and more.

One of additional features

We have a program that turned out after compilation. It can be used as an RPC server. We are running requests over http, and by calling the params function, we can pass the parameter as either a json array or a separate parameter. We will return json, which returns the results of these functions. This is very convenient - the necessary functionality is already built in initially.

Cons of HipHop

Currently, HipHop does not support language constructs and functions such as eval(), create_function() and preg_replace() with /e, although all of these are analogous to eval(). True, in recent releases, in my opinion, you can still enable eval () through config. But it is not recommended to do so. In general, eval() is bad to use.

The main advantages of HipHop

Of course, the main plus is that there is support from Facebook. It works and is quite actively developed. There is a community of developers around this project. A completely new PHP implementation has been written.

As I said, the plus is that native code is generated. An increase in performance is claimed by reducing the cost of loading the processor (tests confirm this).

It is quite flexible in configuration. I was pleasantly surprised that there are quite a few options to customize. I think this is due to the fact that the project really works. Everything that is used is incremented.

As I mentioned, HipHop provides quite a few extras. Among them, use as an RPC server, administration, various statistics, and much more. By the way, there is also an API for working with other languages.

There is some pretty good documentation for this solution. Another advantage: this is a solution that is really ready for use in production (example - Facebook).

Minuses

The main disadvantage is that a rather limited number of modules are currently supported. For example, when working with a database, we can only use MySQL functions. There is no PostgreSQL support here.

There is also such a moment as the complexity of the assembly, which I already mentioned. There are problems with building on 32-bit systems. But I think this will be fixed soon. So far, only compilation from PHP 5.2 is used. Version 5.3 is not yet supported, but will be supported as promised.

What should not be expected from a compilation in general and from HipHop in particular?

Compiling will in no way speed up your slow SQL queries on the database. If the bottleneck is the base, then compile it or don't compile it, there will be no sense from it.

Compiling does not speed up the loading of statics, only in terms of dynamics. It greatly complicates debugging. Probably, many are accustomed to the fact that everything is debugged quite simply in PHP. When compiling, this will no longer work. Although, as I noted, Facebook has made this process as easy as possible, without it it would be even more difficult to compile every time.

Do not expect that this is some kind of "silver bullet" that will solve all your problems. In fact, compilation solves a rather narrow range of problems. If they are, that might help.

What problems does compilation solve?

It reduces the load on the CPU, as active work with PHP at in large numbers requests, the load on it increases quite a lot. Of course, I would like to conduct some tests.

Testing

The first test (the simplest one) is a rather expensive operation that takes a long time to complete. In tests, I tried to abstract, not to make requests using some external resource.

The load falls entirely on the processor. The test showed that HipHop "won" everyone: it works almost one and a half times faster than the standard PHP compiler. PHC passed this test very slowly.

For the second performance test, I took the official PHP script, which can be downloaded from SVN. It performs a number of functions that perform sorting, assignment, summation - quite expensive operations from the point of view of mathematics.

HipHop was ahead again. And with standard PHP, the time difference is about 3 times. PHC performed better here, but about twice as bad as HipHop.

PHP is mainly used for streams that process http requests - it's worth keeping this in mind.

Several standard configurations (Apache with PHP, Nginx with fpm-php and an APC plug-in for code caching). As a fifth option - HipHop.

To be honest, I did not conduct tests on a server, but on a laptop. The numbers, of course, may not fully correspond to reality, but in this case the results are normal. It is worth noting that as the load increases, the number of requests and the total number of requests simultaneously grow. Next is RPS. A standard page was tested, which includes 10 some simple inclusions. In fact, this is testing PHP as an interpreter.

Question from the floor: What are the numbers in the cell - seconds?

Alexey Romanenko: This is fps.

Here we can conclude that with an increase in the number of simultaneous requests, HipHop behaves very well.

It can be seen that using APC is standard practice. It shows that it adds, for example, as Apache, a performance increase of about 2 times. This is also the case with Nginx. But the fact that Nginx is slow does not mean that this bundle is worse. Just a specific test. If we are really testing here, then Apache will "die" on slow requests.

Probably, I want to understand whether we need it or not.

When should you consider compiling?

Most likely, this is necessary when we see that our bottleneck is the CPU. If we're hitting the CPU by using PHP as the standard interpreter, it's probably worth considering that maybe part of the project can be compiled.

For some cases when you need the autonomy of your application to run, this method is hardly suitable.

Reducing the number of servers. When there are a lot of servers, then by reducing the performance by 2 times, roughly speaking, we also reduce the number by half. When this is one server, it makes no sense, but when there are 100-200 of them, then, probably, there is a point.

The main reason why Facebook began to use HipHop is the presence of large amounts of PHP code that cannot be rewritten (either by no one, or simply costly). A 2x increase in performance is already a good thing.

Probably everything. Waiting for questions.

Questions and answers

Question from the floor: Hello. Please tell me if you have any other examples of successful Hiphop implementations besides Facebook. Would you like to translate the RBC website, for example, into HipHop? Alexey Romanenko: I'll start with the second. The RBC website is difficult to translate. Regarding successful implementation. I compiled PHP Unit myself, it compiled successfully. Also, as far as I know, PHP Bunty board compiles successfully. A number of organizations are, in fact, already using compilation. Further tests will show how justified it will be to use this project. Question from the floor: Can you give an example of an organization that uses it? Alexey Romanenko: To be honest, I won't tell you now, but this is the West. As far as I know, no one uses us. Question from the floor: What is the difference at runtime other than the lack of support for some of the features you mentioned. How dangerous is it to translate a "live" project? Alexey Romanenko: The difference is that any compiled program can crash. Perhaps some problems will appear that have not yet been identified. In fact, there are a number of differences in the "behavior" of PHP itself. I did not give them, because more detailed information can be found in the documentation. The Facebook team has written their own interpreter, which is essentially 99.9% equivalent to the one that would work when compiled. It is better to test your code not with a standard PHP interpreter, but, as I said, with hphpi for PHP.

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 set up, 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.

This 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 command is responsible for generating the configuration files that 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 take advantage of 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 PHP compilers that are presented here can recompile 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 dependencies PHP programs and distribute them with desktop functionality (as opposed to regular web applications that run on a server), this is all possible because 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 releases templates that let you create PHP applications with 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.

And which of the proposed compilers 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

Computer