Turned out a few months ago when learning PHP, wrote down the Pyramid, stare at 1 more than a few minutes to see understand. Also depressed at that time how to write out, the original is a regular, and now the code refining the next, write down. Code this thing, really is afraid to forget-_-!!!
Code:
function A ($n) {
$p = ($n + 1)/2; Calculate the middle of the line number
for ($i =1; $i <= $n; $i + +) {
if ($i < $p) {//Traverse upper part
$m = $i-1; Top half: Number of * numbers per line
$SP = ($n-$m)/2; Half the number of spaces
$i 1 = $m; Number of asterisks
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
for ($j =1; $j <= $i 1; $j + +) {echo "*";}//Print * #
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
echo "
";
}else{
if ($i = = $p) {//Traverse middle row: The number of the middle is exactly equal to the number of rows
for ($j =1; $j <= $n; $j + +) {echo "*";}//Print * #
echo "
";
}
else{//traversing the lower half
$i 2 = ($n +1-$i)-1; * Numbered (regular-symmetrical)
$SP = ($n-$i 2)/2; Half the number of spaces
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
for ($j =1; $j <= $i 2; $j + +) {echo "*";}//Print * #
for ($j =1; $j <= $sp; $j + +) {echo "";} Space
echo "
";
}
}
}
}
A (9);
?>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the PHP loop traversal inverted pyramid, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.