Php-Wasm in Javascript

Running PHP & Taking Output

First, import the module and create an instance of PHP:

const { PhpWeb } = await import('https://cdn.jsdelivr.net/npm/php-wasm/PhpWeb.mjs');
const php = new PhpWeb;

Add some event listeners to get ahold of the output:

// Listen to STDOUT & STDERR
php.addEventListener('output', (event) => console.log(event.detail));
php.addEventListener('error',  (event) => console.log(event.detail));

Pass some data on STDIN if required:

php.inputString('This is a string of data provided on STDIN.');

… then run some PHP!

const exitCode = await php.run('<?php echo "Hello, world!";');

Exporting PHP functions to Javascript

With Vrzno enabled, the php.x՝...՝ tagged template string enables you to export almost any PHP value directly to Javascript. This includes arrays, object, and even functions:

const phpStrtotime = await php.x`strtotime(...)`;
const phpDate = await php.x`date(...)`;

const formatTime = (format, time) => phpDate(format, phpStrtotime(time));

You can see this working live on CodePen:

https://codepen.io/SeanMorris227/pen/gONmKqL?editors=1111

(async () => {
  const { PhpWeb } = await import('https://cdn.jsdelivr.net/npm/[email protected]/PhpWeb.mjs');
  const php = new PhpWeb;
  const input  = document.querySelector('#date-input');
  const format = document.querySelector('#date-format');
  const output = document.querySelector('#date-output');

  // Return an actual PHP function to Javascript:
  const phpStrtotime = await php.x`strtotime(...)`;
  const phpDate = await php.x`date(...)`;

  const formatTime = (format, time) => phpDate(format, phpStrtotime(time));

  input.addEventListener('input', () => output.innerText = formatTime(format.value, input.value));
  format.addEventListener('input', () => output.innerText = formatTime(format.value, input.value));

  format.value = 'Y-m-d H:i:s';
  input.value = '8:00pm 2 days ago';

  output.innerText = formatTime(format.value, input.value);
})();

Importing Javascript Values into PHP

The php.x՝...՝ & php.r՝...՝ functions allow for Javascript to be interpolated into PHP code with the ${𝒆𝒙𝒑𝒓𝒆𝒔𝒔𝒊𝒐𝒏} notation that Javascript ՝backtick՝ interpolation normally allows for. Any Javascript values can be interpolated, and PHP will treat them as normal, native objects.

You can see this working live on CodePen:

https://codepen.io/SeanMorris227/pen/wvLJXEJ?editors=0011

(async () => {
  const { PhpWeb } = await import('https://cdn.jsdelivr.net/npm/[email protected]/PhpWeb.mjs');

  const php = new PhpWeb;

  const phpCallback = await php.x` function(){

    $phpString = "PHP String";

    $jsCallback = ${function() {
        return "JS String: " + window.prompt("Provide some input, please.");
    }};

    return sprintf("%s and %s", $phpString, $jsCallback());
  }`;

  document.write(phpCallback());

})();