Advertisement

Online PHP Compiler

Write, run and test PHP code online, right in your browser. This free online PHP compiler needs no installation or signup — just type your code and click Run.

PHP is a server-side scripting language that powers a large share of the web, including WordPress, Laravel apps and countless dynamic websites.

How to use the PHP compiler

  1. Write or paste your PHP code in the editor.
  2. If your program reads input, open the I/O tab and type it into the STDIN box (one value per line).
  3. Click Run — the output and any errors appear in the output panel.

Sample: read input in PHP

<?php
$name = trim(fgets(STDIN));
echo "Hello, $name!\n";

Paste this into the editor, type your name in the STDIN box and click Run.

More PHP examples to try

Arrays and loops

Associative arrays are PHP's most-used data structure.

<?php
$prices = ["Pen" => 20, "Notebook" => 60, "Bag" => 750];

foreach ($prices as $item => $price) {
    echo str_pad($item, 10) . " Rs. " . number_format($price) . "\n";
}

echo "Total: Rs. " . array_sum($prices) . "\n";

Functions and classes

A class with a constructor and typed method (PHP 8).

<?php
class Rectangle {
    public function __construct(private float $w, private float $h) {}

    public function area(): float {
        return $this->w * $this->h;
    }
}

$r = new Rectangle(4, 2.5);
echo "Area: " . $r->area() . "\n";

Common PHP errors and how to fix them

PHP Parse error: syntax error, unexpected end of file
A brace } or quote is missing. Check that every { has a matching }.
Warning: Undefined variable $x
The variable was used before it was assigned, or is misspelled. PHP variables always start with $.
Fatal error: Uncaught Error: Call to undefined function
The function name is misspelled or defined after use inside a condition. Check the name and spelling.
Nothing is printed
Make sure your code starts with <?php and that you echo or print the result.

Frequently asked questions

Is this online PHP compiler free?

Yes. It is completely free to use, with no signup required to write and run code.

Do I need to install anything?

No. It all works from your web browser, so there is nothing to download or set up — it works on Windows, macOS, Linux, Chromebooks and phones.

How do I give input to my PHP program?

Before clicking Run, type the input into the STDIN box under the I/O tab, one value per line. Your program reads it as standard input, just like typing in a terminal.

Is this good for learning PHP?

Yes. It is ideal for practising exercises, testing small programs and trying out ideas from tutorials, because you can run code instantly without configuring an environment.

Other online compilers

Editor powered by OneCompiler.