php.js - PHP VM with JavaScript

This library reads PHP code and transforms it into JavaScript code which can be run in the PHP VM in this library, resulting in same results as with PHP. It starts by tokenizing the PHP code into tokens, which it then uses to build an AST tree. Once the tree has been constructed, the script compiles it into JavaScript that can be interpreted by the VM and then executes it. Any additional unconverted code that gets executed within the VM, such as eval or lambda functions will go through the same process before being executed.

The code can be directly run in your browser, meaning you can execute php code straight out of your browser! It can also be run on node.js, but strongly recommend not to due to potential security issues.

As it might not be as self explanatory, this project is purely a proof-of-concept and should by no means be considered to be used in any sort of production environment.

Some examples


var engine = new PHP ('<?php echo "Hello world!"; ?>');
console.log( engine.vm.OUTPUT_BUFFER); // the outputted buffer from the script

The code is by default run synchronously so the output will be immidiately readable upon the engine finishing execution. Alternatively, the VM can be set to run under a web worker as well, in which case a callback function will need to be provided.


I took a test driven approach to developing this library, which means that I have been mostly focusing on getting the php unit tests (which PHP uses itself) to work correctly. If a certain functionality hasn't been a part of the base unit tests (around 670 in total), it most likely won't work or only works partially. Additionally, only a fraction of module functions have been implemented, and even for the ones that have been, many of them are only partially implemented (i.e. signature checks haven't really been implemented to any functions, although user created functions support them). However, additional functions can easily be added, as they can be written in PHP and have them compiled into JavaScript.

The current status of the passing tests can be seen here. I'll be the first admit that there are still a lot of bugs, but considering the complexity of the language, it is well on its way.

Questions & Comments

If you encounter a bug, please feel free to post it on GitHub. For questions or comments, you can contact me at Twitter @niklasvh or Google Plus