
Run PHP in the browser through Babel - tambourine_man
https://gitlab.com/kornelski/babel-preset-php
======
franciscop
This is cool, it's a tool to transpile it at build time so you are two steps
removed from running PHP on the browser. I dissected this tool a while back
and built another that lets you write and run PHP straight in the browser by
doing the transformation on-the-fly. Wrote a blog post and all around it:

[https://francisco.io/blog/running-php-in-
javascript/](https://francisco.io/blog/running-php-in-javascript/)

Incidentally it's the #3 when you google "Run PHP in Javascript"

~~~
pluc
"phpjs" and "jsphp" each have their share of results as well

------
TazeTSchnitzel
I think “run PHP in the browser” is overstating what this does, it's a naïve
source-to-source translator and will not run any non-trivial PHP application,
because JS and PHP have very different semantics. There are more sophisticated
existing projects that actually try to compile PHP code or the PHP interpreter
for the browser.

~~~
pornel
Author here.

Yes, it's an approximate conversion favoring readable output over exact
semantics, so it won't run WordPress. It might still be useful for smaller
pieces of code. If you want to rewrite a PHP program in JS (which may be a bad
idea…), it will do the most tedious step for you.

I wrote it just as a proof of concept. It was a warm-up before writing a
similar tool for C to Rust source-to-source translation:

[https://lib.rs/citrus](https://lib.rs/citrus)

------
EE84M3i
This is cool. I wonder if anyone has compiled the php interpreter via
emscripten?

Another option might be compiling HPHPc, which I dockerized a few years back:
[https://github.com/allanlw/hphpc-docker](https://github.com/allanlw/hphpc-
docker)

~~~
err4nt
PHP -> WASM, can run anywhere WebAssembly runs: which is all modern browsers,
embeddable into any language with a WASM runtime for it, or usable with a
standalone WASM runtime:

[https://wapm.io/package/php](https://wapm.io/package/php)

~~~
Nextgrid
Can you realistically compile an interpreted language into WASM without
embedding an interpreter? If I remember right, PHP has “eval”, so how would
WASM code deal with such a statement (that could evaluate any possible code)
without embedding a PHP interpreter?

~~~
TazeTSchnitzel
The comment you replied to linked to a WebAssembly build of the PHP
interpreter.

But to answer your question, there's no fundamental difference between
interpreted and compiled languages. If the program you want to compile doesn't
use `eval`, and you can statically determine all the files it would include,
why couldn't you compile it without including an interpreter?

~~~
nicoburns
There is a fundamental difference between dynamically typed and statically
typed languages though. And if you want to AOT compile a dynamically typed
language, then you're going to need to include code that looks awfully like an
interpreter in the final binary.

~~~
jashmatthews
Dynamic typing is really just passing around variables using an enumerated
type. You don't need anything resembling an interpreter to do this and it's
how the C extension APIs for many dynamic languages like Ruby and Lua work.

~~~
chrisseaton
I think people in this thread are using 'dynamic typing' more as a shortcut to
also mean features like _eval_ , which does require a full interpreter or
compiler.

~~~
fiddlerwoaroof
This might be right, but it’s also true that most Common Lisp program can be
compiled to machine code that’s very similar to what a C compiler would
produce, except for the occasional runtime type check.

------
bkanber
This right here is what the internet should be about. Stop debating whether
this makes sense to do. It doesn't, and it doesn't have to. It's an absurd
academic pursuit, and that makes the world a slightly better and more
interesting place.

------
neovive
Very nice! It's interesting that some of the new features released in PHP 7.4
and 8 have direct parallels in JS. Here are a few examples: arrow functions,
spread operator in arrays, weak maps, str_starts_with(), str_ends_with().

~~~
pests
Probably a direct result of cross pollination between the communities. Since
PHP was historically embedded into HTML you would get a lot of front end
technology exposure.

------
DJBunnies
It's been done, and it rarely works for anything non-trivial.

~~~
0x0
I mean, it looks like their version of PHP arrays are very different, even "$a
= $b" doesn't copy the the array, just references the same. I think anyone
using this expecting to write PHP is in for some subtle and not so subtle
surprises.

------
J5892
This is so cool. I hate it.

------
hu3
I like the description:

> Babel preset for converting PHP syntax to JavaScript. It can run subset of
> PHP in the browser or in Node.js. Expanding Atwood's Law.

~~~
hadrien01
I didn't know Atwood's Law, so there it is:

 _> Any application that can be written in JavaScript, will eventually be
written in JavaScript._

[https://blog.codinghorror.com/the-principle-of-least-
power/](https://blog.codinghorror.com/the-principle-of-least-power/) (2007)

~~~
cheez
who would have known JavaScript was Greenspun's tenth rule!
[https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule](https://en.wikipedia.org/wiki/Greenspun%27s_tenth_rule)

------
laurensr
This means we can connect to MySQL from the client side! That will be secure,
right? ;-)

~~~
aikah
> This means we can connect to MySQL from the client side! That will be
> secure, right? ;-)

Not really, the MySQL server would need to support something such as webSocket
or WebRTC to be able to directly connect to a browser (or just plain HTTP). So
you'd still need either to modify MySQL or write a proxy server.

------
joemaller1
Anyone run WordPress through this yet?

------
wh-uws
"...your scientists were so preoccupied with whether or not they could, they
didn't stop to think if they should."

~~~
na85
It's definitely fashionable to hate on PHP but its modern incarnation has no
more warts than JavaScript and the two languages are probably abused by
developers at about the same rate.

Still, it would have been neat to see this headline read "Run Scheme in the
browser through Babel" or similar.

~~~
phendrenad2
I think modern PHP is worlds better than JS, simply because it has a larger
standard library.

~~~
dimitrios1
I'll take Laravel any day over express/sails/hapi/koa

------
aronpye
When all you have is a hammer ...

------
ecmascript
This is icredible impressive, I have always thought php probably could be
translated to js. I could definately see myself using this for future php
projects.

Although, I mostly do javascript today anyway.

~~~
paulpauper
anything done in php in theory can be done in js. the problem is the browser
has to say open a long time

