PHP for loop puzzle -
i have question. trying learn php , told me doing "star" exercises way learn loops. got pretty far myself stuck.
here code ;
for ($row = 0; $row < 11; $row++) { for($y = 0; $y < 1; $y++){ echo "y"; } for($o = 0; $o < 5; $o++){ echo "o"; } if ($o <=5) { echo "</br>"; } }
im trying print following browser ;
y o o o o o o y o o o o o o y o o o o o o y o o o o o o y o o o o o o y o o o o y o o o o y o o o o y o o o o y o o o o y o o o o o
i hope able assist me!
thanks in advance!
$n=11; ($row = 0; $row < $n-1; $row++) { for($line=0; $line < $n;$line++){ if ($row==$line) { echo 'y'; } else echo 'o';} echo '</br>'; } ($row = $n-1; $row > 0; $row--) { for($line=0; $line < $n;$line++){ if ($row==$line) { echo 'y'; } else echo 'o';} echo '</br>'; }
it's start, follow matrix horizontally, , whenever on current line print y. then, start last row towards start. print 2 matrices diagonals.
it can optimized 1 loop goes until half of $n forward y's , @ second half backwards.
Comments
Post a Comment