3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { public array $elements = []; public function __construct(int $howMany) { for ($i = 0; $i < $howMany; $i++) { $this->elements[] = random_int(1, 100); } } } class Demo { public Foo $foo; public function __construct(int $howMany) { $this->foo = new Foo($howMany); } public function demo(): void { echo "\nWe start with ", count($this->foo->elements), " soldiers in \$foo->elements\n"; $this->removeElement(); echo "\nWe now have ", count($this->foo->elements), " soldiers in \$foo->elements\n"; } private function removeElement(): void { array_splice($this->foo->elements, 2, 1); } } $init = random_int(4,10); $demo = new Demo($init); $demo->demo(); $remaining = count($demo->foo->elements); if ($init-1 !== $remaining) { throw new UnexpectedValueException('Wrong number of elements, application is broken'); }
Output for 7.4.0 - 7.4.1, 7.4.4 - 7.4.5, 7.4.7, 7.4.9, 7.4.13, 7.4.24, 8.0.3 - 8.0.5, 8.0.11
We start with 6 soldiers in $foo->elements We now have 5 soldiers in $foo->elements
Output for 7.4.14, 7.4.18, 8.0.9 - 8.0.10
We start with 7 soldiers in $foo->elements We now have 6 soldiers in $foo->elements
Output for 7.4.15, 7.4.20, 8.0.2, 8.0.7 - 8.0.8
We start with 9 soldiers in $foo->elements We now have 8 soldiers in $foo->elements
Output for 7.4.22, 8.0.0, 8.0.6
We start with 10 soldiers in $foo->elements We now have 9 soldiers in $foo->elements
Output for 7.4.8, 7.4.10, 7.4.16, 8.0.1
We start with 4 soldiers in $foo->elements We now have 3 soldiers in $foo->elements
Output for 7.4.2, 7.4.23
We start with 8 soldiers in $foo->elements We now have 7 soldiers in $foo->elements
Output for 7.4.3, 7.4.6, 7.4.11 - 7.4.12, 7.4.19, 7.4.21
We start with 5 soldiers in $foo->elements We now have 4 soldiers in $foo->elements
Output for 7.3.0 - 7.3.31
Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting function (T_FUNCTION) or const (T_CONST) in /in/MjmHv on line 5
Process exited with code 255.

preferences:
63.41 ms | 415 KiB | 5 Q