PHP playground

Run PHP online

Write and run PHP online. A free PHP compiler in your browser: see what echo prints, every warning and error with its line number, and no sign-up.

Code main.php

<?php
// Welcome to the coding.kitty PHP playground.
// Press Run (or Ctrl+Enter) to see what this prints.

$cats = [
    ["name" => "Miko", "age" => 3],
    ["name" => "Nola", "age" => 5],
    ["name" => "Baymax", "age" => 1],
];

// Youngest first.
usort($cats, fn($a, $b) => $a["age"] <=> $b["age"]);

foreach ($cats as $i => $cat) {
    echo ($i + 1) . ". {$cat["name"]} is {$cat["age"]} and says meow\n";
}

$years = array_sum(array_column($cats, "age"));
echo "That's " . count($cats) . " cats, $years years of cat between them.\n";

// Type a name in the Input box and press Run again to see this line.
$name = fgets(STDIN);
if ($name !== false) {
    echo "Hello, " . trim($name) . "!\n";
}
Output

Press Run to see what your code prints.

About this PHP playground

This is a PHP compiler you can use straight away. Write PHP in the editor, press Run, and the output appears next to it: everything your script echoes, every warning, notice and error with its line number, and how long it took.

Your code runs on PHP 8.1 from the command line, in an isolated sandbox on our servers, so it behaves as it would with php main.php on your own machine. It is free, with nothing to install and no account needed.

What you can run

  • PHP 8.1, running one file per run, saved as main.php.
  • Standard library only. PHP's built-in functions for strings, arrays, maths, dates, JSON and regular expressions (preg_match and friends) are all there, with classes, interfaces, enums and closures. The mbstring extension and database drivers are not. There is no package installer, and programs have no network access.
  • Limits: 10 seconds and 256 MB of memory per run. Output past 64 KB is cut off, code can be up to 64 KB and input up to 16 KB.
  • Fair use: 5 runs a minute and 50 a day without an account, 10 a minute when you are signed in.

Reading input

Open the Input box under the editor and type what your script should read. fgets(STDIN) reads the next line, newline included, so trim it. With the box empty, or once every line is read, it returns false.

$name = trim(fgets(STDIN));

Frequently asked questions

Is this online PHP compiler free?

Yes. There is no sign-up and no limit on how much you can write. Signed-out visitors get a quick, usually invisible bot check when they press Run, and a fair-use cap of 5 runs a minute and 50 a day. Signing in raises it to 10 runs a minute.

Which version of PHP does it run?

PHP 8.1 on Linux, run from the command line. Enums, readonly properties, match, named arguments, arrow functions and the nullsafe operator all work. Features added after 8.1, such as readonly classes and typed class constants, do not.

Can I run a web page, a form or Laravel?

No. Your script runs once from the command line, as php main.php, not behind a web server, so there is no $_GET, $_POST or session, and no Composer packages or network. Any HTML outside the <?php tags is printed as it is, which shows you what a page would send to the browser.

Why do I see warnings and deprecation notices?

Every error level is switched on, as it is on a development machine, and sent to the error output so it does not mix with what your script prints. A warning such as an undefined variable does not stop the script; a fatal error does.

How do I read input in the PHP playground?

Type it in the Input box under the editor, then read it from STDIN: fgets(STDIN) returns one line, fscanf(STDIN, "%d", $n) reads a number, and stream_get_contents(STDIN) reads everything at once. Stick to these rather than mixing them with readline().

Why did my script stop with a time-out?

Each run gets 10 seconds and 256 MB of memory. A program that goes past either is stopped. The usual cause is a while loop whose condition never turns false.

Is my code saved or shared?

Your code is kept in your own browser so it is still there next time, and nowhere else. When you press Run it is sent to our code runner, run once in a sandbox, and thrown away. We do not store it or show it to anyone.

Keep going

Also in the playground: Python, JavaScript, TypeScript, SQL, Bash, C, C++, Go, Rust, Java, C#, Ruby, Lua, R, Dart, Swift.