npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

phpscriptjs

v0.1.0

Published

PHPScript — real PHP syntax, compiled to JavaScript. Toy language + live browser playground.

Readme

PHPScript

Real PHP syntax, compiled to JavaScript. A toy language where you write actual .php files — <?php ... ?>, <?= ... ?>, HTML templates, classes, foreach, the whole vibe — and the compiler spits out portable JavaScript you can run with Node or in the browser.

<?php
$langs = ['PHP', 'JS', 'TypeScript'];
foreach ($langs as $l): ?>
  <li><?= strtoupper($l) ?></li>
<?php endforeach; ?>

Install

npm install -g phpscriptjs

This installs a php binary.

Heads up — the CLI is literally named php. That's part of the joke. If real PHP is already on your $PATH, whichever comes first wins. If you want both, alias PHPScript to something else:

alias phps='$(npm bin -g)/php'   # or wherever npm put it

Quick start

# Run a file
php hello.php

# Or explicitly
php run hello.php

# Compile to .js
php build src/ -o dist/

# Watch mode
php watch src/

# Launch the live browser playground at http://localhost:5173
php serve

Inline one-liners work too:

php -e '<?php echo "Hello " . strtoupper("world") . "!";'

Browser playground

The repo ships with a live playground: PHPScript on the left, rendered HTML (or raw output, or compiled JS) on the right, recompiled as you type.

npm run dev
# or: php serve --port 5173

Then open http://localhost:5173. Share a snippet by clicking Share link — the code is encoded into the URL hash.

Language features

Tags & output

  • <?php ... ?>, <?= expr ?>, short <? ... ?>
  • echo, print
  • Trailing ?> is optional

Literals

  • Numbers, true/false/null
  • 'single' strings (no interpolation)
  • "double" strings with $var and {$expr} interpolation
  • Arrays: [1, 2, 3] -> JS array, ['a' => 1] -> JS object, mixed -> object

Variables

  • $name maps 1:1 to JS $name
  • Reserved-word collisions (e.g. $class) are mangled safely

Operators

  • Arithmetic, comparison (==, ===, !=, !==), logical (&&, ||, !, and, or, xor)
  • String concat . compiles to JS + with string coercion
  • Null coalescing ??, ternary ?:, Elvis ?:, spaceship <=>
  • Pre/post ++/--, compound assigns (+=, .=, ...)

Control flow

  • if / elseif / else, both brace and alternative if: / elseif: / else: / endif; forms
  • while, do-while, for, foreach (both as $v and as $k => $v)
  • switch/case/default, break, continue, return

Functions & classes

  • function f($a, $b = 1, ...$rest) — defaults + rest
  • Anonymous: function ($x) use ($y) { ... }, arrow: fn ($x) => $x * 2
  • class Foo extends Bar { public $x = 1; function m() { $this->x; } } → ES6 class
  • $obj->m(), $obj->prop, Foo::staticM(), new Foo(...), self::, parent::, static::, $this

Superglobals & builtins (in src/runtime.js)

  • $_GET, $_POST, $_SERVER, $_ENV (populated from process.env in Node, empty in the browser)
  • Strings: strlen, strtoupper, strtolower, trim, ltrim, rtrim, str_replace, substr, explode, implode/join, sprintf (minimal), str_repeat, str_pad, str_contains, str_starts_with, str_ends_with, number_format, ucfirst, lcfirst, ucwords, htmlspecialchars
  • Arrays: count, array_map, array_filter, array_reduce, array_keys, array_values, in_array, array_push, array_pop, array_shift, array_unshift, array_merge, array_reverse, array_slice, array_sum, array_unique, range, sort
  • Types & misc: isset, empty, is_array, is_string, is_numeric, is_int, is_bool, is_null, intval, floatval, strval, boolval, abs, min, max, round, floor, ceil, print_r, var_dump, json_encode, json_decode, date, time

Example: examples/template.php

<?php
$users = [
  ['name' => 'Ada',  'age' => 36],
  ['name' => 'Alan', 'age' => 41],
];
function greet($u) { return "Hi, " . $u['name'] . "!"; }
?>
<!doctype html>
<h1>Users (<?= count($users) ?>)</h1>
<ul>
<?php foreach ($users as $u): ?>
  <li><?= greet($u) ?> — age <?= $u['age'] ?></li>
<?php endforeach; ?>
</ul>

Run it:

php examples/template.php

Compiles to (roughly):

const { __echo, __str, __entries, count } = require('/abs/path/to/runtime.js');
let $users = [
  { name: 'Ada',  age: 36 },
  { name: 'Alan', age: 41 },
];
function greet($u) { return __str('Hi, ') + __str($u['name']) + __str('!'); }
__echo("<!doctype html>\n<h1>Users (");
__echo(count($users));
__echo(")</h1>\n<ul>\n");
for (const $u of __values($users)) {
  __echo("\n  <li>");
  __echo(greet($u));
  __echo(" — age ");
  __echo($u['age']);
  __echo("</li>\n");
}
__echo("\n</ul>\n");

Not supported (explicit non-goals)

PHPScript is a demo, not a migration tool. It doesn't implement:

  • PHP's full type-juggling rules for == (JS == is used as an approximation)
  • References (&$x) — parsed, treated as normal bindings
  • Traits, interfaces, enums, attributes
  • Generators / yield
  • preg_* regex family, full sprintf format specifiers
  • Namespaces, use imports, autoloading
  • Any of the standard PHP extensions (MySQL, GD, curl, ...)

If you need real PHP, use real PHP.

Architecture

.php source
   │
   ▼
┌──────┐   tokens   ┌────────┐   AST   ┌─────────┐   JS
│Lexer │──────────▶│ Parser │────────▶│ Codegen │───────▶ Node (stdout)
└──────┘            └────────┘         └─────────┘        Browser (iframe)
                                                          │
                                           runtime.js ────┘
  • Lexer (src/lexer.js): dual-mode. Emits RAW_TEXT tokens for HTML between PHP tags; flips into code mode on <?php / <?= / <? and back on ?>.
  • Parser (src/parser.js): hand-written recursive-descent + Pratt expression parser with proper PHP precedence.
  • Codegen (src/codegen.js): walks the AST and emits JavaScript. Variable hoisting, string interpolation to template literals, foreach to for ... of, classes to ES6 classes, etc.
  • Runtime (src/runtime.js): the PHP standard library, implemented in plain JS. Imported by generated code, inlined into the browser bundle.
  • CLI (src/cli.js): run, build, watch, serve.
  • Playground (web/): static HTML + CodeJar editor + Prism highlighting + sandboxed <iframe> preview. Recompiles on every keystroke.

Scripts

npm test            # run the example test suite
npm run build:web   # bundle the browser-safe compiler via esbuild
npm run dev         # shortcut for `php serve`

License

MIT