3v4l.org

run code in 300+ PHP versions simultaneously
<?php $max = 13; // The last number $cols = 4; // The point at which a new line will start $arr = array_chunk(range(1, $max), $cols); // Magic ;D // Print the data. foreach ($arr as $key => $row) { // In case we are wrapping on the far side, this will prevent the row from // starting on the left. $row = array_pad($row, $cols, ' '); // This will reverse every other row $row = ($key % 2 === 0) ? $row : array_reverse($row); foreach ($row as $value) { $value = str_pad($value, strlen($max), ' ', STR_PAD_LEFT); echo "{$value} "; } echo "\n"; }
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
1 2 3 4 8 7 6 5 9 10 11 12 13

preferences:
135.86 ms | 407 KiB | 5 Q