Skip to content
Online Compiler

Online PHP Compiler

Write PHP and run it right here. PHP runs inside your browser, so nothing you type is uploaded. The first run downloads the runtime.

Ready

<?php
// fgets(STDIN) reads one line from the input box.
$name = trim(fgets(STDIN) ?: "world");
echo "Hello, $name!\n";

for ($i = 1; $i <= 3; $i++) {
    echo "$i squared is ", $i * $i, "\n";
}

Type the input before you run. Each fgets(STDIN) call reads the next line.

Output

Press Run or Ctrl+Enter to see the output here.

Runs in your browser with PHP 8.4 (php-wasm 0.2.0). Your code is not sent to a server.

How to run PHP code here

  1. Write or paste your PHP code into the editor. There is a short example in it to start from.
  2. If your program reads input, type it into the input box first — one value per line.
  3. Press Run or Ctrl+Enter. The output appears next to the editor, with errors shown in red.

How this page runs PHP

Runtime: PHP 8.4 (php-wasm 0.2.0)

PHP runs as PHP 8.4 compiled to WebAssembly by the php-wasm project (version 0.2.0). The first run downloads about 2.5 MB from the jsDelivr CDN, and later visits load it from the browser cache. Your code is read the way PHP reads a .php file: anything outside <?php … ?> is printed as it is, so begin a script with <?php. Warnings, notices and uncaught exceptions appear as errors with their line number in main.php.

Everything happens on your device. The code you write is never uploaded, logged or stored on a server. Each run stops after 10 seconds and keeps up to 100,000 characters of output, so an endless loop cannot freeze the page.

What is not available

This is PHP as a command-line script, not a web server: there are no $_GET or $_POST values, no headers, no database server and no Composer. Only the extensions built into this runtime are present — json, pcre, ctype, bcmath, date and Reflection work, while mbstring, intl, gd and curl are not included. Each run starts a fresh PHP process, so functions and classes from the last run are gone.

PHP examples

Open an example in the editor above, change it and run it. Opening an example does not run it.

FizzBuzz with match

<?php
for ($i = 1; $i <= 15; $i++) {
    echo match (0) {
        $i % 15 => "FizzBuzz",
        $i % 3 => "Fizz",
        $i % 5 => "Buzz",
        default => $i,
    }, PHP_EOL;
}

Associative arrays and array functions

<?php
$prices = ["tea" => 3.5, "cake" => 4.25, "coffee" => 2.8];

arsort($prices);
foreach ($prices as $item => $price) {
    printf("%-8s %5.2f\n", $item, $price);
}

$cheap = array_filter($prices, fn ($p) => $p < 4);
echo "cheap: ", implode(", ", array_keys($cheap)), PHP_EOL;
echo "total: ", array_sum($prices), PHP_EOL;
print_r(array_map(fn ($p) => round($p * 1.1, 2), $prices));

Enums and readonly properties

<?php
enum Suit: string
{
    case Hearts = "♥";
    case Spades = "♠";
}

final class Card
{
    public function __construct(
        public readonly string $rank,
        public readonly Suit $suit,
    ) {}

    public function __toString(): string
    {
        return $this->rank . $this->suit->value;
    }
}

$hand = [new Card("A", Suit::Spades), new Card("10", Suit::Hearts)];
echo implode(" ", $hand), PHP_EOL;

String functions and regular expressions

<?php
$text = "The quick brown fox jumps over the lazy dog";

echo strtoupper($text), PHP_EOL;
echo str_word_count($text), " words", PHP_EOL;
echo ucwords(strrev($text)), PHP_EOL;
echo str_pad("centered", 20, "*", STR_PAD_BOTH), PHP_EOL;
echo preg_replace('/\b(\w)(\w*)\b/', '$2$1ay', "hello world"), PHP_EOL;
echo json_encode(["words" => explode(" ", $text)], JSON_PRETTY_PRINT), PHP_EOL;

Reading every line from STDIN

<?php
// Read every line of the input box, then sum the numbers.
$lines = [];
while (($line = fgets(STDIN)) !== false) {
    $lines[] = (int) trim($line);
}
echo count($lines), " numbers, sum ", array_sum($lines), PHP_EOL;
echo "max ", max($lines), PHP_EOL;

Input

4
8
15
16
23
42

Reading input in PHP

Programs here do not stop and wait for you to type while they run. Everything your program reads comes from the input box, which you fill in before pressing Run.

fgets(STDIN) reads the next line from the input box — trim() it to drop the newline — and returns false at the end. readline() works too and removes the newline for you. stream_get_contents(STDIN) reads everything at once, and fscanf(STDIN, "%d %d") reads two numbers from a line.

Saving and sharing

Your latest PHP code and input are saved automatically in this browser, so they are still here when you come back. Nothing is saved on a server.

Share creates a link that carries your code inside the address itself, compressed after the # sign. Whoever opens it sees your code in the editor, and it only runs when they press Run.

Keyboard shortcuts

KeysAction
Ctrl+EnterRun the code
Ctrl+/Comment or uncomment the line
Tab · Shift+TabIndent or outdent the selection
Ctrl+FFind and replace in the code
Ctrl+Z · Ctrl+Shift+ZUndo / redo
Esc → TabLeave the editor with the keyboard

On a Mac, use ⌘ instead of Ctrl.

Frequently asked questions

Which PHP version is this?

PHP 8.4, so enums, readonly properties, match, named arguments, first-class callable syntax and property hooks are all available. The exact version is shown under the editor.

Can I test a web page or a form here?

Only as text. The code runs like php script.php on the command line, so echo and HTML outside the PHP tags are printed to the output as plain text. There is no web server, so $_GET, $_POST, cookies and sessions between runs do not exist.

Can I use Composer, Laravel or mbstring?

No. There is no Composer and no way to add packages, and frameworks such as Laravel need a server. The mbstring extension is not part of this runtime either; for UTF-8 text, preg functions with the u modifier work.

Is this PHP compiler free?

Yes. There is no account, no sign-up and no daily limit. The only limit is that a single run stops after 10 seconds.

Is my PHP code sent to a server?

No. The PHP runtime is downloaded into your browser and your code runs there, on your device. The site only serves the page and the runtime files; it never receives the code you write or the input you give it.

Where is my code saved?

In this browser's local storage, one draft per language. It stays until you clear this site's data, and it is not synced to other devices. Use a share link to move code somewhere else.

How do I share my PHP code?

Press Share and send the copied link. The code is compressed into the part of the link after #, which browsers never send to a server. Very long programs do not fit in a link, and the page will tell you when that happens.

Does it work on a phone or tablet?

Yes. It runs in any current mobile browser. On a slow connection the first run takes longer because the runtime has to be downloaded once.

Run another language