<?php
echo "Welcome to Online IDE!\n";
echo "Start coding in PHP...";
?>

Press Run or Ctrl ↵ to execute main.php.
Type program input here while it runs.

PHP

Online PHP Compiler

Scripts run with the PHP 8.4 command-line interpreter as php -f main.php, and run settings switch to 8.3 or 8.5. fgets(STDIN) and fscanf(STDIN, ...) read what you type in the console while the script runs. There’s no web server, so echo prints to the console rather than to a page.

Try it above

How PHP runs here

Versions
PHP 8.3, 8.4 (default), 8.5 (CLI)
Run
php -f main.php
Limits per run
256 MB memory, 15 s of CPU time on half a core, 2 MB of output
Time
Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
Sandbox
Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.

A script that reads input

This counts the words in a sentence and lists the most common:

PHP
<?php
echo "Enter a sentence: ";
$text = trim(fgets(STDIN));
$words = str_word_count(strtolower($text), 1);
$counts = array_count_values($words);
arsort($counts);

printf("%d words, %d unique\n", count($words), count($counts));
foreach (array_slice($counts, 0, 3, true) as $word => $n) {
    echo str_pad($word, 8), $n, PHP_EOL;
}
Terminal
$ php -f main.php
Enter a sentence: The cat and the dog and the bird
8 words, 5 unique
the     3
and     2
cat     1

Reading input in PHP

fgets(STDIN) returns one line including its newline, so wrap it in trim(). fscanf() reads a line by a format, and explode() splits one into an array:

PHP
<?php
echo "Two numbers: ";
[$a, $b] = fscanf(STDIN, "%d %d");
echo "Sum: ", $a + $b, "\n";
echo "Some numbers: ";
$nums = array_map('intval', explode(' ', trim(fgets(STDIN))));
echo "Max: ", max($nums), "\n";
Terminal
$ php -f main.php
Two numbers: 4 9
Sum: 13
Some numbers: 3 17 8
Max: 17
  • fscanf() returns an array, which [$a, $b] = unpacks. intval() and (int) turn the strings from explode() into numbers.
  • stream_get_contents(STDIN) and file_get_contents('php://stdin') read to the end of input, which the console can’t send. Read line by line instead.
  • $_GET, $_POST and HTML forms don’t exist on the command line. Program arguments from run settings arrive in $argv, starting at $argv[1].
  • Text before <?php is printed as is. A file that doesn’t start with <?php prints its own code instead of running it.

Several files with require

Paths are relative to the folder the script runs in, which holds every playground file:

PHP
// helpers.php
<?php
function money(float $x): string { return number_format($x, 2); }

// main.php
<?php
require "helpers.php";
echo money(1234.5), "\n";
Terminal
$ php -f main.php
1,234.50
  • require stops the script when the file is missing; include warns and carries on. require_once skips a file that’s already loaded.
  • Classes can load themselves with spl_autoload_register(), which needs no Composer.
  • Run starts the open tab, so set Entry file in run settings to main.php while you edit helpers.php.

What isn’t available

  • Composer packages: there’s no composer install and no vendor/autoload.php.
  • A MySQL server. There’s no database server and no network, and mysqli isn’t installed, so mysqli_connect() fails with Fatal error: Uncaught Error: Call to undefined function mysqli_connect(). PDO with an in-memory SQLite database works, as below.
  • Extensions the official PHP build leaves out by default, such as gd and intl. mbstring, ctype, json and PDO with SQLite are built in.
  • A browser page: HTML that the script echoes shows as text in the console.

Practicing SQL from PHP with PDO and SQLite:

PHP
<?php
$db = new PDO('sqlite::memory:');
$db->exec("CREATE TABLE books (title TEXT, year INTEGER)");
$add = $db->prepare("INSERT INTO books VALUES (?, ?)");
foreach ([['Dune', 1965], ['Neuromancer', 1984]] as $row) {
    $add->execute($row);
}
foreach ($db->query("SELECT title, year FROM books ORDER BY year") as $book) {
    echo "{$book['title']} ({$book['year']})\n";
}
Terminal
$ php -f main.php
Dune (1965)
Neuromancer (1984)

Errors you’re likely to see

PHP prints its messages into the console with the output, along with the file and line. Warnings let the script continue; a fatal error ends it with Return Code 255.

  • “Parse error: syntax error, unexpected token "echo"”: the line before is missing its semicolon.
  • “Warning: Undefined variable $total”: the variable was used before anything was assigned to it, as in $total += $i; with no $total = 0; first.
  • “Warning: Undefined array key 3”: the array has no index 3. Check with isset($a[3]) or count($a).
  • “Fatal error: Uncaught DivisionByZeroError: Division by zero”: a division, intdiv() or % by zero. Since PHP 8 this is an exception, not a warning.
  • “Fatal error: Uncaught TypeError: Unsupported operand types: string + int”: arithmetic on a string that isn’t a number, such as "abc" + 1.

After a failed run, Fix with AI explains the error and proposes a change you review before it applies.

Newer PHP worth knowing

  • PHP 8.0 to 8.3, available on every offered version: match expressions, named arguments, str_contains(), enums, readonly properties, and json_validate() from 8.3.
  • PHP 8.4, the default: array_find(), array_any() and array_all(), property hooks, and new MyClass()->method() without wrapping parentheses.
  • PHP 8.5 adds the pipe operator, $value |> trim(...) |> strtoupper(...), which passes a value through functions left to right.

Questions

Can I run PHP with HTML and forms?

No. Scripts run on the command line, so there’s no web server, $_GET or $_POST. HTML that the script echoes appears as text.

Which PHP version is it?

PHP 8.4 by default, with 8.3 and 8.5 in run settings.

How do I read user input in PHP?

Use trim(fgets(STDIN)) for a line, or fscanf(STDIN, "%d %d") to read numbers. The script pauses until you type in the console and press Enter.

Can I connect to MySQL?

No. Runs have no network and no database server. Use PDO with new PDO('sqlite::memory:') to practice SQL from PHP.

Can I use Composer packages?

No, there’s no Composer. Split your own code into files and load them with require.

Why does my code print as text instead of running?

The file doesn’t start with <?php, so PHP treats it as output. Put <?php on the first line.