<?php
$owidth = 4;
$oheight = 3;
$final = -1;
$wstep = $hstep = 0;
$nos = ($oheight * $owidth);
function calc($step,$check,$change,$last) {
for($i=$step;$i<$check;++$i){
$last = $last + $change;
echo ' '.$last;
}
return($last);
}
while($nos > 0){
// accross
if ($wstep < $owidth) {
$final = calc($wstep,$owidth,1,$final);
$nos = $nos - ($owidth - $wstep);
++$hstep;
// up
if ($hstep < $oheight) {
$final = calc($hstep,$oheight,$owidth,$final);
$nos = $nos - ($oheight - $hstep);
++$wstep;
// back
if ($wstep < $owidth) {
$final = calc($wstep,$owidth,-1,$final);
$nos = $nos - ($owidth - $wstep);
++$hstep;
// down
if ($hstep < $oheight) {
$change = 0-$owidth;
$final = calc($hstep,$oheight,$change,$final);
$nos = $nos - ($oheight - $hstep);
}
}
}
}
++$wstep;
}
?>