PHP loop traversal inverted pyramid

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.