Skip to content
AgileLogics

PHP Editor

Write and edit PHP with syntax highlighting, right in your browser.

Loading editor…

What PHP is used for

PHP is a server-side scripting language for building websites and web applications. It renders HTML pages, handles form submissions, talks to databases such as MySQL and PostgreSQL, and powers JSON APIs. WordPress, Drupal, Laravel and Symfony are written in it, and it also runs command-line scripts and scheduled jobs.

This page is a quick place to write or tidy a PHP file without opening an IDE. The highlighter understands that a .php file mixes two languages: HTML outside the PHP tags is coloured as HTML, and code after the opening PHP tag is coloured as PHP, so start every file with that tag. Alongside that you get bracket matching, find and replace, word wrap, a dark theme, fullscreen, copy, and Download, which saves the file as index.php.

The page has no PHP runtime. It won't execute your script, show echo output or report a parse error.

Try it with some PHP 8 code

This snippet uses a backed enum (PHP 8.1), a match expression, a readonly class with constructor property promotion (PHP 8.2) and numeric literal separators:

<?php

declare(strict_types=1);

enum Status: string
{
    case Paid = 'paid';
    case Pending = 'pending';
    case Refunded = 'refunded';

    public function label(): string
    {
        return match ($this) {
            Status::Paid => 'Paid in full',
            Status::Pending => 'Awaiting payment',
            Status::Refunded => 'Money returned',
        };
    }
}

final readonly class Invoice
{
    public function __construct(
        public string $number,
        public int $amountInCents,
        public Status $status,
    ) {}
}

$invoices = [
    new Invoice('INV-001', 12_500, Status::Paid),
    new Invoice('INV-002', 4_990, Status::from('pending')),
];

foreach ($invoices as $invoice) {
    printf(
        "%s: %s (%s)\n",
        $invoice->number,
        number_format($invoice->amountInCents / 100, 2),
        $invoice->status->label(),
    );
}

Run on PHP 8.2 or newer, it prints:

INV-001: 125.00 (Paid in full)
INV-002: 49.90 (Awaiting payment)

Things that trip people up

  • declare(strict_types=1) has to come first. The PHP manual requires it to be the very first statement in the file. Only the opening tag may precede it. It also affects calls made from that file only, not the whole program.
  • Leave off the closing ?> tag. PSR-12 says it must be omitted from files containing only PHP. A stray newline after ?> becomes output and triggers "headers already sent".
  • Loose comparison changed in PHP 8. 'abc' == 0 was true in PHP 7 and is false from PHP 8.0, because a non-numeric string is now compared as a string (PHP 8.0 migration notes). Numeric strings still convert, so '1e3' == '1000' is true. Use === unless you really want conversion.
  • Quotes matter. Variables are interpolated in double quotes and heredoc, never in single quotes or nowdoc, so 'Total: $sum' prints the literal text $sum.

PSR-12 also sets the layout most projects follow: 4-space indentation, opening braces for classes and methods on their own line, and braces for if/foreach on the same line, as in the example above.

Running the file on your machine

  1. Click Download. The file saves as index.php; rename it if you like.
  2. Install PHP if php -v doesn't print a version.
  3. Check the syntax with php -l index.php, then run it with php index.php.
  4. For a page that outputs HTML, start PHP's built-in server in that folder with php -S localhost:8000 and open http://localhost:8000/, which serves index.php by default. The built-in server is meant for development only.

Learn more PHP

If you're starting out, the free PHP course begins with your first PHP script. The PHP cheat sheet covers syntax at a glance, and the PHP developer roadmap lays out what to learn after the basics, from Composer to frameworks.

Last updated

Frequently asked questions

Can this editor run PHP?
No. There's no PHP interpreter or web server behind the page, so echo output and errors won't appear. Download the .php file and run it with php index.php, or serve a folder with php -S localhost:8000.
Why is my PHP code not highlighted?
The editor treats the file like a real .php file: everything before an opening <?php tag is HTML. Start the file with <?php and the code after it is highlighted as PHP.
Should I end the file with a closing ?> tag?
Not in a file that contains only PHP. PSR-12 says the closing tag must be omitted from such files, because any whitespace after it is sent as output and can break headers or downloads.
Which PHP versions does the highlighting understand?
It handles modern PHP 8 syntax such as enums, match expressions, readonly properties, named arguments and attributes. It highlights code without checking it, so run php -l on the downloaded file to find syntax errors.
Is my code stored anywhere?
No. It stays in your browser tab and isn't uploaded or saved. Copy or download it before closing the page.