PHP
v8.2.3
PHP
v8.2.3
PHP is a dynamically typed, server-side scripting language created by Rasmus Lerdorf in 1994, originally as a set of tools for personal home pages. It was designed for embedding logic directly into HTML and generating dynamic web content. Over time it gained full object-oriented support, namespaces, closures, and a large standard library, evolving into a general-purpose language with a strong focus on the web.
Today PHP powers a large share of the web, including platforms like WordPress, Drupal, and frameworks such as Laravel and Symfony. Its dependency manager Composer and the Packagist registry support a vast package ecosystem. This compiler runs PHP 8.2.3, so you can use modern features like typed properties, enums, named arguments, the nullsafe operator, and just-in-time compilation.
<?php
echo "Hello, World!\n";<?php
$name = trim(fgets(STDIN));
echo "Hello, $name!\n";<?php
$fruits = ["apple", "banana", "cherry"];
foreach ($fruits as $i => $fruit) {
echo ($i + 1) . " " . $fruit . "\n";
}<?php
function factorial($n) {
return $n <= 1 ? 1 : $n * factorial($n - 1);
}
echo factorial(5) . "\n";<?php
function divide($a, $b) {
if ($b === 0) {
throw new InvalidArgumentException("Cannot divide by zero");
}
return $a / $b;
}
try {
echo divide(10, 0);
} catch (InvalidArgumentException $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
$text = "banana";
$counts = [];
foreach (str_split($text) as $ch) {
$counts[$ch] = ($counts[$ch] ?? 0) + 1;
}
print_r($counts);<?php
$s = " Hello, World ";
echo strtoupper(trim($s)) . "\n";
$nums = [3, 1, 2];
sort($nums);
echo implode(", ", $nums) . "\n";<?php
class Person {
public function __construct(
public string $name,
public int $age
) {}
public function describe(): string {
return "$this->name ($this->age)";
}
}
$p = new Person("Ada", 36);
echo $p->describe() . "\n";<?php
$nums = [1, 2, 3, 4, 5];
$evens = array_filter($nums, fn($n) => $n % 2 === 0);
$doubled = array_map(fn($n) => $n * 2, $evens);
echo implode(", ", $doubled) . "\n";
echo "Sum: " . array_sum($nums) . "\n";<?php
enum Suit: string {
case Hearts = 'H';
case Spades = 'S';
public function color(): string {
return match($this) {
Suit::Hearts => 'red',
Suit::Spades => 'black',
};
}
}
echo Suit::Hearts->color() . "\n";Yes. CompileBytes runs PHP in your browser without installing the PHP interpreter locally.
It runs PHP 8.2.3, so you can use enums, readonly properties, named arguments, and other modern PHP 8 features.
Read with fgets(STDIN) in CLI mode and type your input into the stdin box before running the script.
Yes, running PHP on CompileBytes is free and requires no account.
Yes, PHP code must begin with the <?php tag; anything before it is treated as plain output text.