241

I know that PHP is compiled to byte code before it is run on the server, and then that byte code can be cached so that the whole script doesn't have to be re-interpreted with every web access.

But can you "compile" PHP code and upload a binary-ish file, which will just be run by the byte code interpreter?

5
  • A related question: PHP compiler for Windows
    – Palec
    Commented Feb 9, 2014 at 17:16
  • 3
    Please do not tag this with hhvm nor hiphop. HHVM is not an inherent part of the question, just a possible answer. meta.stackexchange.com/a/149347/238706
    – Palec
    Commented Jul 22, 2014 at 22:07
  • @HalayemAnis I think you are mentioned a wrong link.. Can you provide the correct link for that tutorial? or any latest update guys?
    – CJ Ramki
    Commented Jun 20, 2015 at 15:15
  • @CJRamki sorry, please use this link COMPILE_PHP Commented Jun 20, 2015 at 21:05
  • This question is actually rather vague. You could "simply" build a PHP compiler that compiles (in its fullest meaning) your source code and a PHP interpreter into an ELF, EXE or whatever. Commented Aug 19, 2015 at 9:37

15 Answers 15

249

After this question was asked, Facebook launched HipHop for PHP which is probably the best-tested PHP compiler to date (seeing as it ran one of the world’s 10 biggest websites). However, Facebook discontinued it in favour of HHVM, which is a virtual machine, not a compiler.

Beyond that, googling PHP compiler turns up a number of 3rd party solutions.

PeachPie

  • PeachPie GitHub
  • compiles PHP to .NET and .NET Core
  • can be compiled into self-contained binary file
  • runs on Mac, Linux, Windows, Windows Core, ARM, ...

Phalanger

  • GitHub (download), Wikipedia
  • compiles to .NET (CIL) looks discontinued from July 2017 and doesn't seem to support PHP 7.

phc

  • compiles to native binaries
  • not very active now (February 2014) – last version in 2011, last change in summer 2013

Roadsend PHP Compiler

bcompiler

  • PECL extension of PHP
  • experimental
  • compiles to PHP bytecode, but can wrap it in Windows binary that loads PHP interpreter (see bcompiler_write_exe_footer() manual)
  • looks discontinued now (February 2014) – last change in 2011

Project Zero

  • Wikipedia, IBM
  • incubator of changes for WebSphere sMash
  • supported by IBM
  • compiles to Java bytecode
  • looks discontinued now (February 2014) – website down, looks like big hype in 2008 and 2009

Bambalam

  • compiles to stand-alone Windows binaries
  • the binaries contain bytecode and a launcher
  • looks discontinued now (February 2014) – last change in 2006

BinaryPHP

  • compiles to C++
  • looks discontinued now (February 2014) – last change in 2003
8
  • What about Shared web host? Could I compile my PHP script with one of those compilers and then upload to my linux based web host?
    – SaidbakR
    Commented May 14, 2013 at 20:37
  • 2
    @sємsєм it depends on your host, but if I had to guess, I'd guess many hosts wouldn't allow it. Commented May 21, 2013 at 0:39
  • 2
    None of those projects above seems to be maintained anymore. Is there a generally accepted compiler since the last 2 years or why are all those projects disregarded/abandoned?
    – Preexo
    Commented Jul 4, 2013 at 14:35
  • A nice list of PHP compilers is at thefreecountry.com. All the tools mentioned there are included in this answer now.
    – Palec
    Commented Feb 9, 2014 at 17:20
  • 2
    Since these all seem dead, the answer seems to be "no".
    – Ira Baxter
    Commented Nov 6, 2016 at 0:43
22

The short answer is "no".

The current implementation of PHP is that of an interpreted language. You can argue the theoretical aspects of the fact that any language can technically be interpreted or compiled, but as it stands, the current implementations are such that PHP code requires an interpreter to run, and the interpreter manages the executing environment.

To answer your question about uploading pre-compiled PHP bytecode, it's probably possible, but you'd have to implement a way for the PHP interpreter to read in such a file and work with it. With existing opcode caches out there already, it doesn't seem like a task that would reap much reward.

7
  • 3
    Its often the case that you do need an interpreter to properly run PHP programs, but that doesn't mean a compiler can't provide it in the compiled code. phc (phpcompiler.org) handles all the problems you describe. To the best of my knowledge, Roadsend (roadsend.com) does too. Commented Sep 26, 2009 at 13:04
  • 30
    This answer is out of date -- HipHop was released 6 months after it was posted, in Feb. 2010. Commented Jul 30, 2011 at 0:50
  • 1
    @FrankFarmer Have you had any experience with HipHop? Does HipHop work properly with include and require? For example if a PHP script is usually included with include( 'controller/' . $controller_name . '.php' ) will this automagically work with HipHop? cheers
    – Lea Hayes
    Commented Dec 29, 2011 at 23:33
  • 3
    Actually you can compile PHP code, using regular PHP compilers. It just depends on the target platform; you can compile into Zend opcode, C language or into .NET assemblies (using Phalanger php-compiler.net) Commented Apr 11, 2012 at 18:33
  • 3
    The short answer is "no". The current implementation of PHP is that of an interpreted language. You can argue the theoretical aspects… There’s nothing theoretical about it, plenty of interpreted languages have compilers; viz AutoIt/AutoHotkey, or even as far back as BASIC. There are lots of programs that have been written in those, then compiled for use by the public as standalone programs. There is nothing special about PHP to prevent it from being compilable, which is why there exist a bunch of compilers, but unlike AHK, there’s no official, tested, compiler—Zend Guard doesn’t quite count.
    – Synetech
    Commented Jul 19, 2015 at 21:05
15

Since the question was first asked, there has been a change to that answer from a flat out "no" to a "kind of"

http://github.com/facebook/hiphop-php/wiki

Hip Hop for PHP was a compiler that took PHP code and turned it into highly optimized C++ Apparently, some functions are not supported (for example 'explode')

I found this question while looking for more information on how to implement HipHop and thought I'd speak up :)

Since 2013 Facebook no longer use it, however, and it has been discontinued in favour of HHVM, which is not a compiler: https://en.wikipedia.org/wiki/HipHop_for_PHP

4
  • 3
    this is what facebook uses I believe
    – Richard H
    Commented Dec 19, 2010 at 0:00
  • The "kind of" lies now a few years behind and since HHVM (HipHop Php) is getting better and better every day, there finaly is a way (I think). Using HHVM it is nowadays possible to pre-analyze the php code. As a result you get a Binary-Cache File. With this file and the correct HHVM configuration, it is possible to run a Webpage only with bytecode, no need for the source files anymore ;)<br>I hope I got it right.
    – PKeidel
    Commented Apr 11, 2014 at 7:11
  • @Andrea, what do you mean by HHVM being deprecated? It looks maintained and alive as a project. Or do you mean that the name changed from Hip Hop for PHP to HHVM? Then talking about deprecation is misleading.
    – Palec
    Commented Jan 28, 2016 at 6:29
  • 1
    @Palec HipHop for PHP was discontinued. It was a PHP-to-C++ compiler that Facebook used, but it produced unwieldy output (multi-GB binaries) and couldn't support the full language. HHVM is a successor project which shares some of the same code, but is not a compiler. HHVM is a virtual machine. Facebook no longer support HipHop.
    – mystery
    Commented Jan 28, 2016 at 13:23
11

There is also

which aims

  • To encode entire script in a proprietary PHP application
  • To encode some classes and/or functions in a proprietary PHP application
  • To enable the production of php-gtk applications that could be used on client desktops, without the need for a php.exe.
  • To do the feasibility study for a PHP to C converter

The extension is available from PECL.

8

phc allows you to compile PHP programs into shared libraries, which can be uploaded to the server. The PHP program is compiled into binaries. It's done in such a way as to support evals, includes, and the entire PHP standard library.

8

Um, anybody heard of Zend Guard, which does exactly what this person is asking. It encodes/obfuscates PHP code into "machine code".

3
  • 1
    I don't think they wanted obfuscation as much as the speedups resulting from interperting bytecode rather than source.
    – Stephen
    Commented May 1, 2012 at 14:51
  • @Stephen Zend Guard is meant to do that too.
    – Gajus
    Commented Oct 17, 2013 at 16:19
  • 1
    Zend Guard decided not to support PHP 7 which is drawback - blog.zend.com/2016/10/10/zend-guard-and-php-7/#.W7YISnszaM8
    – v.j
    Commented Oct 4, 2018 at 12:32
8

If you are simply looking for producing a binary executable from a PHP script, then please avoid trying to make your question extremely precise because it will make it appear that you know exactly what you need. Besides, most PHP developer have absolutely zero clue about what a bytecode is.

With that said, the answers is YES. I have just finished compiling a PHP script into a binary. And not just any binary. I have used the CDE application (link to Wayback Machine, the original link is now broken) to turn it into an portable binary that can be distributed with all the dependencies and executed without any issue… and it works beautifully.

All you need is to use phc.

0
3

Actually, the Just-In-Time compiler introduced with PHP 8 does in fact compile PHP. Strangely enough, it doesn't really speed up CMS based websites (e.g. WordPress), however, it does open the doors for PHP to compete with the likes of C++. For more information, see the RFC behind the JIT implementation here: https://wiki.php.net/rfc/jit. Also, Matthew Weir O'Phinney has posted a number of insightful blogs that shed light on its capabilities. Start reading here: https://www.zend.com/blog/exploring-new-php-jit-compiler.

2

There are several "compilers" of PHP code. Most of them do not support all of PHP features, since these simply must be interpreted during run time.

We are using Phalanger - http://www.php-compiler.net/ - that is supporting even those dirty PHP dynamic features, and still is able to compile them as .NET assembly, that can be distributed as a standalone DLL.

1

see 5.5.x with the integrated OPcache module, volatile in a shared memory, much more performance and the dynamism principle of php remain untouched.

http://www.php.net/manual/en/opcache.installation.php

1

In php 7 there is the php ini option opcache.file_cache that saves the bytecode in a specific folder. In could be useful to in php cli script that are "compiled" and saved in a specific folder for a optimized reuse.

Opcache it is not compiling but is something similar.

1
  • "In php 7" its not PHP7 only, it's PHP 5.5.0 and later Commented Feb 9, 2019 at 12:23
1

If you are allowed to run real native binaries, then this is your compiler:

https://github.com/ircmaxell/php-compiler

It's a PHP compiler written in PHP!

It compiles PHP code to its own VM code. This VM code can then either be interpreted by its own interpreter (also written in PHP, isn't that crazy?) or it can be translated to Bitcode. And using the LLVM compiler framework (clang and co), this Bitcode can be compiled into a native binary for any platform that LLVM supports (pretty much any platform that matters today). You can choose to either do that statically or each time just before the code is executed (JIT style). So the only two requirements for this compiler to work on your system is an installed PHP interpreter and an installed clang compiler.

If you are not allowed to run native binaries, you could use the compiler above as an interpreter and let it interpret its own VM code, yet this will be slow as you are running a PHP interpreter that itself is running on a PHP engine, so you have a "double interpretation".

0

PHP doesn't really get compiled as with many programs. You can use Zend's encoder to make it unreadable though.

4
  • doesn't it get compiled in the same way that perl gets compiled? Commented Sep 11, 2009 at 0:26
  • PHP is compiled on the server on the fly I believe. I have used an encoder to hide the source code though. If that is what you are worried about. They work really well.
    – Joe
    Commented Sep 11, 2009 at 0:34
  • nah, I was just interested in the theoretical aspect of it Commented Sep 11, 2009 at 4:12
  • 2
    PHP is not complied, its an interpreted language. the cache contains 'opcodes' whitch php interprets (not having to read text), php code cannot be 'run' directly as you would with c
    – borrel
    Commented Aug 5, 2011 at 9:06
0

There is also bcgen (a PHP7 port of bcompiler):

https://github.com/vjardin/bcgen/

(PHP7.2 only)

0

I'm surprised no one has mentioned IonCube. WHMCS uses it effectively in production and has been doing so for years. But it's not exactly what you want.

If you just want to run a PHP app on a Desktop, PHPDesktop might be for you.

Other than that, the most modern answer was already mentioned, which is OpCache.

(Maybe share a bit more as to why you want to convert it to Bytecode)

Not the answer you're looking for? Browse other questions tagged or ask your own question.