3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Based { private array $arr = ["zero", "one", "two", "three", "four", "five"]; public function removeElementFromArr (int $index): void { array_splice($this->arr, $index, 1); if ($index === 0) { throw new Exception("Cannot remove the first element of an array, reason: https://www.youtube.com/watch?v=bvlSQz7cBr0"); } } public function getArr (): array { return $this->arr; } public function setArr (array $newArr) { $this->arr = $newArr; } } $obj = new Based(); var_dump($obj->getArr()); $obj->removeElementFromArr(3); var_dump($obj->getArr()); $obj->removeElementFromArr(0);
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
array(6) { [0]=> string(4) "zero" [1]=> string(3) "one" [2]=> string(3) "two" [3]=> string(5) "three" [4]=> string(4) "four" [5]=> string(4) "five" } array(5) { [0]=> string(4) "zero" [1]=> string(3) "one" [2]=> string(3) "two" [3]=> string(4) "four" [4]=> string(4) "five" } Fatal error: Uncaught Exception: Cannot remove the first element of an array, reason: https://www.youtube.com/watch?v=bvlSQz7cBr0 in /in/44QFC:10 Stack trace: #0 /in/44QFC(27): Based->removeElementFromArr(0) #1 {main} thrown in /in/44QFC on line 10
Process exited with code 255.

preferences:
67.56 ms | 407 KiB | 5 Q