PHP · Playground
PHP playground
Running PHP inside the browser is not available yet. Use this editor to write, then download the file and run it locally with PHP 8.4. The example snippets were verified that way.
<?php
// A tiny till receipt: arrays, a loop, string formatting.
$order = [
['item' => 'Sourdough loaf', 'qty' => 2, 'price' => 4.20],
['item' => 'Cinnamon knot', 'qty' => 3, 'price' => 2.75],
['item' => 'Oat cookie', 'qty' => 1, 'price' => 1.90],
];
$total = 0.0;
foreach ($order as $line) {
$cost = $line['qty'] * $line['price'];
$total += $cost;
echo str_pad($line['item'], 16), 'x', $line['qty'], str_pad(number_format($cost, 2), 9, ' ', STR_PAD_LEFT), "\n";
}
echo str_repeat('-', 27), "\n";
echo str_pad('Total', 18), str_pad(number_format($total, 2), 9, ' ', STR_PAD_LEFT), "\n";
Output
Press Run to see the program’s output here.
Running PHP inside the browser is not available yet. Use this editor to write, then download the file and run it locally with PHP 8.4. The example snippets were verified that way.
PHP examples
Each one was run with PHP 8.4 before it was published. Load one, change it, run it.
- PHP · Count down with for, walk a map with foreach
<?php // Count down the last five launch seconds, then lift off. for ($second = 5; $second >= 1; $second--) { echo "T-minus $second\n"; } echo "Lift-off\n"; // foreach walks any array, giving you key and value together. $tides = ['06:12' => 'high', '12:30' => 'low', '18:45' => 'high']; foreach ($tides as $time => $state) { echo "$time $state tide\n"; } - PHP · A typed function with a default and named arguments
<?php // Typed parameters, a default value and a return type. function shelfLabel(string $title, int $copies = 1, bool $reserved = false): string { $label = strtoupper($title) . " ($copies)"; return $reserved ? "$label RESERVED" : $label; } echo shelfLabel('Tide tables'), "\n"; echo shelfLabel('Knot guide', 3), "\n"; // Named arguments (PHP 8.0+) let you skip the middle parameter. echo shelfLabel('Harbour map', reserved: true), "\n"; - PHP · array_map, array_filter and sort on a list
<?php // Transform, filter and sort a list without writing a single loop. $readings = [21.5, 19.0, 24.8, 18.2, 23.1]; $rounded = array_map(fn(float $c): int => (int) round($c), $readings); echo 'Rounded: ', implode(', ', $rounded), "\n"; // array_filter keeps the original keys, so implode is a safe way to print it. $warm = array_filter($rounded, fn(int $c): bool => $c >= 22); echo 'Warm: ', implode(', ', $warm), "\n"; sort($rounded); echo 'Sorted: ', implode(', ', $rounded), "\n"; echo 'Average: ', round(array_sum($readings) / count($readings), 1), "\n"; - PHP · Everyday string functions
<?php // Everyday string tools: length, search, case, split and join. $subject = 'Re: invoice 2041 overdue'; echo strlen($subject), " characters\n"; echo str_contains($subject, 'overdue') ? "flagged\n" : "normal\n"; echo ucwords(strtolower('welcome TO the harbour')), "\n"; $parts = explode(' ', $subject); echo 'Words: ', count($parts), "\n"; echo implode('-', array_reverse($parts)), "\n"; echo sprintf('[%-8s|%8s]', 'left', 'right'), "\n"; echo sprintf('%06.2f', 3.14159), "\n"; - PHP · A class with promoted constructor properties
<?php // Constructor property promotion (PHP 8.0+) keeps small classes short. class Locker { private array $items = []; public function __construct(private string $code, private int $capacity) { } public function store(string $item): bool { if (count($this->items) >= $this->capacity) { return false; } $this->items[] = $item; return true; } public function describe(): string { return "Locker {$this->code}: " . count($this->items) . "/{$this->capacity} used"; } } $locker = new Locker('B7', 2); foreach (['helmet', 'gloves', 'boots'] as $item) { echo $item, ': ', $locker->store($item) ? 'stored' : 'no room', "\n"; } echo $locker->describe(), "\n"; - PHP · match expression for status codes
<?php // match (PHP 8.0+) returns a value and compares strictly, unlike switch. $codes = [200, 404, 503, 418]; foreach ($codes as $code) { $label = match (true) { $code >= 500 => 'server error', $code >= 400 => 'client error', $code >= 200 && $code < 300 => 'success', default => 'other', }; echo "$code -> $label\n"; }
How this page was checked. Every program on it was run with PHP 8.4 at build time by the publishing checks, and the output shown is what it printed. Running PHP inside the browser is not available yet, so the Run button is absent rather than pretending; copy the code and run it with PHP 8.4 locally.