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! |