Basic php Tutorial-for loop printing hollow diamond

Source: Internet
Author: User
Tags php programming language
Basic php Tutorial-for loop printing hollow diamond
This section describes examples of for loop printing hollow diamond and for loop application for your reference.

In the previous section, we introduced the php for loop. in this section, we will look at some specific examples. Idea: 1. write the code and print the solid pyramid. 2. modify the code and leave the pyramid blank. 3. modify the code and reverse the pyramid. 4. modify the reversed code, empty the pyramid. 5. modify the code and parameterize the specific number.

1. print the pyramid of solid gold:

     ";}?>

2. empty the pyramid

     ";}?>

3. reverse the pyramid

     ";}// Invert // by bbs.it-home.org for ($ I = 5; $ I >=0; $ I --) {// Print spaces for ($ j = 0; $ j <= 5-$ I; $ j ++) {echo "" ;}// print * for ($ k = 1; $ k <= 2 * $ I-3; $ k ++) {echo "*";} echo"
";}?>

4. empty the pyramid and remove the * sign in the middle to modify the code (if ($ I = 1 | $ I = 5) {// remove $ I = 5 and empty the center)

     ";}// By bbs.it-home.org // reverse throwing blank for ($ I = 5; $ I >=0; $ I --) {// Print spaces for ($ j = 0; $ j <= 5-$ I; $ j ++) {echo "" ;}// print * for ($ k = 1; $ k <= 2 * $ I-3; $ k ++) {// question of how to empty the space and * ($ k = 2 * $ I-3 | $ k = 1) {echo "*";} else {echo "" ;}} echo"
";}?>

5. parameterize the specific number: replace the specific number 5 with $ n = 5, and then set the value of $ n to zoom in or out.

     ";}// Reverse the blank for ($ I = $ n; $ I >=0; $ I --) {// Print the space for ($ j = 0; $ j <= $ n-$ I; $ j ++) {echo "" ;}// print * for ($ k = 1; $ k <= 2 * $ I-3; $ k ++) {// question of how to empty the space and * ($ k = 2 * $ I-3 | $ k = 1) {echo "*";} else {echo "" ;}} echo"
";}?>

Thanks for your attention to the php getting started tutorial. this series of basic php tutorials will help new php beginners to quickly master the php programming language. The programmer's home will continue to provide php tutorials for you and wish you a better learning experience!

Related Article

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.