下面是生成上面三种星号图案方法。
function nbspStar(int $num = 0,$star = '******'): string
{
$result = '';
for ($i = 0; $i < $num; $i++) {
$spaces = str_repeat(' ';, $i);
$result .= $spaces . $star . "<br>";
}
return $result;
}
$mystar = nbspStar(5);
echo $mystar;
function ascStar(int $num = 0): string
{
$result = '';
$j = $num;
for ($i = 1; $i <= $num; $i++) {
$spaces = str_repeat(' ';, $j - 1);
$star = str_repeat('*', 2*$i - 1);
$result .= $spaces . $star . "<br>";
$j--;
}
return $result;
}
$mystar = ascStar(5);
echo $mystar;
function descStar(int $num = 0): string
{
$result = '';
$j = 0;
for ($i = $num; $i > 0; $i--) {
$spaces = str_repeat(' ';, $j);
$star = str_repeat('*', 2*$i - 1);
$result .= $spaces . $star . "<br>";
$j++;
}
return $result;
}
$mystar = descStar(5);
echo $mystar;