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;
}
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.
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.
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.
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.
Open an example in the editor above, change it and run it. Opening an example does not run it.
<?php
for ($i = 1; $i <= 15; $i++) {
echo match (0) {
$i % 15 => "FizzBuzz",
$i % 3 => "Fizz",
$i % 5 => "Buzz",
default => $i,
}, PHP_EOL;
}
<?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));
<?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;
<?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;
<?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
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.
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.
| Keys | Action |
|---|---|
| Ctrl+Enter | Run the code |
| Ctrl+/ | Comment or uncomment the line |
| Tab · Shift+Tab | Indent or outdent the selection |
| Ctrl+F | Find and replace in the code |
| Ctrl+Z · Ctrl+Shift+Z | Undo / redo |
| Esc → Tab | Leave the editor with the keyboard |
On a Mac, use ⌘ instead of Ctrl.
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.
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.
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.
Yes. There is no account, no sign-up and no daily limit. The only limit is that a single run stops after 10 seconds.
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.
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.
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.
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.