3v4l.org

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

preferences:
70.72 ms | 415 KiB | 5 Q