Skip to main content

PHP (Wasm)

PHP in Browser, powered by WebAssembly (using php-wasm).

php-wasm runs PHP in the browser using WebAssembly. This matches the behavior of the official PHP interpreter and allows using PHP's standard library. However, it requires relatively large download and has limited capabilities for client-side DOM manipulation.

Note

If you need a lighter-weight interpreter with more capable client-side DOM manipulation in PHP and do not need to use PHP's standard library, you may want to use the PHP interpreter written in JavaScript.

Usage

Standard Library

The PHP standard library is supported.

<?php
phpinfo();
show code
import { createPlayground } from 'livecodes';

const options = {
"config": {
"script": {
"language": "php-wasm",
"content": "<?php\n\nphpinfo();"
},
"activeEditor": "script",
"mode": "result"
}
};
createPlayground('#container', options);

JavaScript Interoperability

JavaScript Interoperability is achieved via the VRZNO php extension.

Example:

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"phpwasm": "<?php\n\n// read from DOM\n$oldTitle = vrzno_eval('document.querySelector(\"#title\").innerText');\necho $oldTitle;\n\n$newTitle = 'Changed@' . date('h:i:s');\n\n// set DOM properties\nvrzno_eval('document.querySelector(\"#title\").innerText = \"' . $newTitle . '\"' );\n\n// run console.log\nvrzno_eval('console.log(\"Hello, World!\")');\n",
"html": "<h1 id=\"title\">Hello, PHP!</h1>\n",
"console": "open"
}
};
createPlayground('#container', options);

Check the starter template for another example.

Language Info

Name

php-wasm

Extension

.wasm.php

Alias

phpwasm

Editor

script

Compiler

php-wasm

Version

php-wasm v0.0.7, running PHP v8.2.4

Code Formatting

Using prettier and Prettier PHP Plugin.

Example Usage

show code
import { createPlayground } from 'livecodes';

const options = {
"template": "php-wasm"
};
createPlayground('#container', options);

Starter Template

https://livecodes.io/?template=php-wasm