<?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";
}