How to use the PHP compiler
Write a script in the editor or pick one from Examples: input and arithmetic, arrays, a prime check, strings, a word count, recursion, a class, interfaces and inheritance, exceptions, and matrix multiplication. A script starts with <?php. Press Run or Ctrl+Enter. echo, print and printf write to the output, and warnings and errors appear where the php command prints them, in the output.
Your code, input and tests are saved on this device as you type. Share makes a link with the code and input inside it, and Download saves the code as main.php.
How input works
The Input tab is the script's standard input. trim(fgets(STDIN)) reads the next line, fscanf(STDIN, "%d %d", $a, $b) reads values from a line, and readline() reads a line too. Use one way of reading in a script: fgets reads ahead into PHP's own buffer, as it does on the command line, so readline would not see those lines. When the input runs out, fgets returns false.
What runs your code
PHP 8.4, the official interpreter, compiled to WebAssembly by the php-wasm project and run in your browser. The language and its core functions are all there, with strict_types, enums, match and readonly properties. Integers are 32-bit here: PHP_INT_MAX is 2147483647, and larger results turn into floats. There is no web server, so $_GET, $_POST and sessions are empty, and no database or network. The first run downloads PHP once, about 3 MB, and your browser keeps it for next time.
Each run may take 10 seconds; then it is stopped. Output is kept up to 10,000 lines or 1 MB, and the rest is cut with a notice. Programs cannot reach the network, and no packages or libraries can be installed: what a language brings with it is what you can use. A loop that never ends is stopped at 10 seconds, keeping what it printed.
Questions
- Which version of PHP is this?
- PHP 8.4, the official interpreter compiled to WebAssembly.
- Why does a big number turn into a float?
- PHP here is a 32-bit build, so integers stop at 2147483647. Past that, PHP switches to floats, as it does on any 32-bit system. Keep values smaller, or use floats on purpose.
- Can I use $_GET, $_POST or a database?
- No. Scripts run like the php command on its own, with standard input and output, and no web server, network or database.
- Is there a step-by-step visualizer for PHP?
- Not yet: PHP runs on its own interpreter here, which the visualizer cannot follow. Python, C, C++, Java, JavaScript and TypeScript have one.
See also
- PHP Programming: PHP 8.4 from the first program to arrays, functions, closures, classes, interfaces, traits and exceptions, run in your browser.
- Practice problems, checked against hidden tests.
- Online Python compiler
- Online C compiler
- Online C++ compiler
- Online Java compiler
- Online SQL compiler
- Online JavaScript compiler
- Online TypeScript compiler
- Online 8086 Assembly compiler
- Online Lua compiler
- Online Ruby compiler
- Online Go compiler