Input n Height=2*n+1;
if n=1
*
**
*
if n=2
*
* *
* * * * *
* *
*
if n=3
*
* *
* * *
* * * * * * * * *
* * *
* *
*
and so on...
thare is a gap of 'n' stars between the two vertical lines of stars.
Yummy foreach:
<?php
function draw_stars($n)
{
foreach(range(1, $n) as $i) {
foreach(range(1, $i) as $k)
$pattern[] = '*';
$pattern[] = str_repeat('*', 2 * $i + 1);
}
foreach($pattern as $k => $stars)
$pattern[$k] = str_split(
str_pad($stars, 2 * $n + 1, ' ', STR_PAD_BOTH)
);
$pattern = transpose($pattern);
foreach($pattern as $line)
echo implode('', $line), "
";
}
function transpose($array) {
return array_map(null, ...$array);
}