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' == 0wastruein PHP 7 and isfalsefrom 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'istrue. 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
- Click Download. The file saves as
index.php; rename it if you like. - Install PHP if
php -vdoesn't print a version. - Check the syntax with
php -l index.php, then run it withphp index.php. - For a page that outputs HTML, start PHP's built-in server in that folder with
php -S localhost:8000and openhttp://localhost:8000/, which servesindex.phpby 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