3v4l.org

run code in 300+ PHP versions simultaneously
<?php $matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], // [17, 18, 19, 20] ]; $r = 1; matrixRotation($matrix, $r); function matrixRotation($matrix, $r) { $m = new Matrix($matrix); $m->rotate(); print_r($m->get()); } class Matrix { private $mx; private $m; private $n; private $a; // i [a,b] private $b; private $c; // j [c,d] private $d; public function get() { return $this->mx; } public function __construct($mx) { try { $this->set($mx); } catch(\Exception $e) { echo $e->getMessage(); // exit(1); } } private function set(array $mx) { $this->m = count($mx); $this->n = count($mx[0]); $this->a = 0; $this->b = m-1; $this->c = 0; $this->d = n-1; $this->mx = $mx; } public function rotate($r) { $k = 0; while(1) { $this->a += $k; $this->b -= $k; $this->c += $k; $this->d -= $k; if ( $this->a === $this->b || $this->c === $this->d ) { break; } $mult = 2*($this->m-2*$k)+2*($this->n-2*$k)-4; $rm = $r % $mult; while($rm--) { rotateSquare(); } ++$k; } } private function rotateSquare($k) { $t = []; //transite for($i=0; $i<$this->m; ++$i) { for($j=0; $j<$this->n; ++$j) { $t[$i][$j] = [0,0]; } } $j=$this->c; for($i=$this->a; $i<$this->b; ++$i) { $t[$i][$j] = [-1,0]; } $i=$this->b; for($j=$this->c; $j<$this->d; ++$j) { $t[$i][$j] = [0,1]; } $j=$this->d; for($i=$this->b; $i>$this->a; --$i) { $t[$i][$j] = [1,0]; } $i=$this->a; for($j=$this->d; $j<$this->c; --$j) { $t[$i][$j] = [0,-1]; } $temp = []; for($i=0; $i<$this->m; ++$i) { for($j=0; $j<$this->n; ++$j) { $temp[$i + $t[$i][$j][0] ] [$j + $t[$i][$j][1] ] = $this->mx[$i][$j]; } } $this->mx = $this->ksort($temp); return $this; } private function ksort($temp) { foreach($temp as &$ar){ ksort($ar); } ksort($temp); return $temp; } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught Error: Undefined constant "m" in /in/RWh4Y:48 Stack trace: #0 /in/RWh4Y(36): Matrix->set(Array) #1 /in/RWh4Y(14): Matrix->__construct(Array) #2 /in/RWh4Y(11): matrixRotation(Array, 1) #3 {main} thrown in /in/RWh4Y on line 48
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught Error: Undefined constant "m" in /in/RWh4Y:48 Stack trace: #0 /in/RWh4Y(36): Matrix->set(Array) #1 /in/RWh4Y(14): Matrix->__construct(Array) #2 /in/RWh4Y(11): matrixRotation(Array, 1) #3 {main} thrown in /in/RWh4Y on line 48
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: Use of undefined constant m - assumed 'm' (this will throw an Error in a future version of PHP) in /in/RWh4Y on line 48 Warning: A non-numeric value encountered in /in/RWh4Y on line 48 Warning: Use of undefined constant n - assumed 'n' (this will throw an Error in a future version of PHP) in /in/RWh4Y on line 50 Warning: A non-numeric value encountered in /in/RWh4Y on line 50 Fatal error: Uncaught ArgumentCountError: Too few arguments to function Matrix::rotate(), 0 passed in /in/RWh4Y on line 15 and exactly 1 expected in /in/RWh4Y:55 Stack trace: #0 /in/RWh4Y(15): Matrix->rotate() #1 /in/RWh4Y(11): matrixRotation(Array, 1) #2 {main} thrown in /in/RWh4Y on line 55
Process exited with code 255.

preferences:
190.98 ms | 402 KiB | 181 Q