Php output pyramid two methods

Source: Internet
Author: User
The following summarizes two ways to implement Pyramid printing. one is to use a user-defined function, and the other is to use a for loop, in fact, both are used, but the former is more advanced. the code for customizing the function implementation pyramid is as follows :? Php * nbsp... the following summarizes two ways to implement Pyramid printing. one is to use a user-defined function, and the other is to use a for loop, in fact, both are used, but the former is more advanced.

User-defined function implementation pyramid. the code is as follows:

 20) {return "must be between 1 and 20";} if ($ rows! = (Int) ($ rows) {return 'The number of rows must be an integer';} $ str = ""; if ($ sort) {for ($ I = 1; $ I <= $ rows; $ I ++) {$ str. ='
'; For ($ j = 1; $ j <= $ I; $ j ++) {if ($ j = 1) {for ($ k = 1; $ k <= ($ rows-$ I); $ k ++) {$ str. = ''; }}$ str. = '*'. '';}} else {for ($ I = $ rows; $ I >=1; $ I --) {$ str. ='
'; For ($ j = 1; $ j <= $ I; $ j ++) {if ($ j = 1) {for ($ k = 1; $ k <= ($ rows-$ I); $ k ++) {$ str. = ''; }}$ str. = '*'. '';}}return $ str;} echo fun_py (9, false );

Next we will implement a pyramid shape object, which also uses the for loop. the code is as follows:

 = $ A; $ B --) {echo "" ;}for ($ c = 1; $ c <=$ B; $ c ++) {echo "*". "";} echo"
";}


The code is as follows:

 = 1; $ a --) {for ($ B = 10; $ B >=$ a; $ B --) {echo "" ;}for ($ c = 1; $ c <= $ B; $ c ++) {echo "*". "";} // open-source phprm.com echo"
";}


Address:

Reprinted at will, but please attach the article address :-)

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.