: This article mainly introduces PHP loop traversal inverted pyramid. if you are interested in the PHP Tutorial, refer to it. When I learned php a few months ago, I wrote the inverted pyramid. I saw it for more than a minute. I was still depressed about how I wrote it at the time. it turned out to be regular. now I have refined the code and noted it down. The code is really afraid to forget -_-!!!
Code:
Function a ($ n ){
$ P = ($ n + 1)/2; // calculate the middle row number
For ($ I = 1; $ I <= $ n; $ I ++ ){
If ($ I <$ p) {// traverse the upper half
$ M = 2 * $ I-1; // upper part: number of * numbers per line
$ Sp = ($ n-$ m)/2; // half of the number of spaces
$ I1 = $ m; // Number of stars
For ($ j = 1; $ j <= $ sp; $ j ++) {echo "" ;}// space
For ($ j = 1; $ j <= $ i1; $ j ++) {echo "*";} // PRINT *
For ($ j = 1; $ j <= $ sp; $ j ++) {echo "" ;}// space
Echo"
";
} Else {
If ($ I = $ p) {// traverse the middle row: the number in the middle is equal to the number of rows.
For ($ j = 1; $ j <= $ n; $ j ++) {echo "*";} // PRINT *
Echo"
";
}
Else {// traverse the lower half
$ I2 = 2 * ($ n + 1-$ I)-1; // * number (regular-symmetric)
$ Sp = ($ n-$ i2)/2; // half of the number of spaces
For ($ j = 1; $ j <= $ sp; $ j ++) {echo "" ;}// space
For ($ j = 1; $ j <= $ i2; $ j ++) {echo "*";} // PRINT *
For ($ j = 1; $ j <= $ sp; $ j ++) {echo "" ;}// space
Echo"
";
}
}
}
}
A (9 );
?>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces PHP loop traversal inverted pyramid, including some content, hope to be helpful to friends who are interested in PHP tutorials.