Running PHP in Javascript 🤯

Francisco Donate 📝 Edit 🕑

But why? you are wondering. You must be given some context: this is an experiment inside another experiment I'm doing to learn about Static Site Generators. PHP is the first language I knew well and I wanted to test whether it's a horrible or just a bad idea to use it for templates.

If you come from Google trying to find how to run PHP inside Javascript, you are very likely lost. Beware, this article is only for experts! /s

Since a large motivator for my site generator is to rely purely on Node.js and not to have to install Ruby, I also don't want to install PHP! But can you run PHP inside Javascript? I only need the most basic PHP features: variables, echo, loops.

The sarcastic language of this article might be deceiving, but there is a working demo below.

Initial objective

The idea came when I was making FrontMatter work properly for the current file. It is also related to my general dissatisfaction with the Node.js template engines, but that a topic for another day:

title: Running PHP in Javascript
  <meta content="{{title}}" property="og:title" />

It reminded me quite a bit to what can be achieved with raw PHP:

<? $title = "Running PHP in Javascript"; ?>
  <title><?= $title ?></title>
  <meta content="<?= $title ?>" property="og:title" />

Googling around

Google just led me to 780 StackOverflow questions with -1 average score. After reading some, I believe the average score should be more like -5. I also learned that devs are trying to do:

alert("<?= "Hello world" ?>");

As an astute reader might notice, that is supposed to run first through the PHP server, then sent to the browser and finally run the resulting alert("Hello world") on the client. But this level of sophistication is too high, I just want to run raw PHP on the browser! I want something like this:

// No server needed, "just" Javascript parsing PHP
alert(php(`<?= "Hello world" ?>`));

But not even Reddit's "PHP" search in r/atwoodslaw could find what I seek.

Promising finds

After getting lucky with a "PHP parser Javascript" search, I end up finding the package php-parser in Github/npm. The problem is that the output is an Abstract Syntax Tree. I just wanted to run the code, not whatever an abstract tree seems to be:

An abstract tree somehow

Since you are reading this it means that in the Related Projects category I found my next clue. I head to the website with the funny looking cat: babel-preset-php. It transpiles PHP into Javascript 🎉

Since babel is in total chaos with the whole babel-loader vs @babel/loader and no one knows which one is which (ironic, innit?), I give up and take a break. I do practice my Kanjis, speak with my family in Spanish and think about the English title for this article.

Building up the library

Fresh again, I scavenge the library babel-preset-php for my project. I am now using:

I wasn't going to show you, but since my code is so tiny thanks to those great libraries I can just paste it here for your satisfaction:

import parser from 'php-parser';
import translator from './translator';
import generator from '@babel/generator';

const run = (code, opts) => {
  // Make a closure so that `out` doesn't collide with the PHP variables:
  let out = '';
  // Define `echo` since it's used in the transpiled JS code for some reason
  opts.echo = opts.echo || (str => out += str);
  // Pretend this is safe. Pro tip: IT IS NOT SAFE
  new Function(...Object.keys(opts), code)(...Object.values(opts));
  return out;

export default function (src, opts = {}) {
  const ast = new parser().parseCode(src);
  const file = translator.translateProgram(ast);
  const code = generator(file).code;
  return run(code, opts);

Later on I will export some individual parts for better testing, but that is really all there is to it.


The long-awaited demo! Turn your internet off, hide your kids and say your prayers. I already loaded the php() function for you, but are you ready to run it?

Tips to make the demo better for lack of documentation:


Yup, this sucks. Use it just as an example of things that should never happen. If you really, really need to use this, and want more features, feel free to throw me loads of money and I might consider talking you out of it or contacting the relevant health authorities.

Now that we have reached this point and seeing how many languages have AST generators, I ponder: can any language be transpiled to Javascript? Should we do it? Webassembly is going to eat JS so we might just give it a try!

If you hate PHP, or Javascript, and you really want to let me and others know* feel free to comment on Hacker News: