PHP exercises-print hollow diamond with for Loop

Source: Internet
Author: User

PHP exercises-print hollow diamond with for Loop

Idea: 1. Write the code and print the solid pyramid.

2. modify the code and empty the pyramid.

3. modify the code and reverse the pyramid

4. Modify the reversed code and empty the pyramid.

5. modify the code and parameterize the specific number.

1. Write the code to print the pyramid of solid gold:

<? Php // $ n = 5; for ($ I = 1; $ I <= 5; $ I ++) {// print spaces for ($ j = 1; $ j <= 5-$ I; $ j ++) {echo "& nbsp;" ;}// print * for ($ k = 1; $ k <= 2 * $ I-1; $ k ++) {echo "*" ;}echo "<br/>" ;}?>

 

2,

2. Improve code and empty the pyramid

<? Php // $ n = 5; for ($ I = 1; $ I <= 5; $ I ++) {// print spaces for ($ j = 1; $ j <= 5-$ I; $ j ++) {echo "& nbsp;" ;}// print * for ($ k = 1; $ k <= 2 * $ I-1; $ k ++) {// print the last line after the first line and connect * if ($ I = 1 | $ I = 5) {echo "*";} else {// question of how to empty the space and * if ($ k = 1 | $ k = 2 * $ i-1) {echo "*";} else {echo "& nbsp;" ;}} echo "<br/>" ;}?>

 

3. modify the code and reverse the pyramid

<? Php // $ n = 5; for ($ I = 1; $ I <= 5; $ I ++) {// print spaces for ($ j = 1; $ j <= 5-$ I; $ j ++) {echo "& nbsp;" ;}// print * for ($ k = 1; $ k <= 2 * $ I-1; $ k ++) {// print the last line after the first line and connect * if ($ I = 1 | $ I = 5) {echo "*";} else {// question of how to empty the space and * if ($ k = 1 | $ k = 2 * $ i-1) {echo "*";} else {echo "& nbsp;" ;}} echo "<br/>" ;}// reverse for ($ I = 5; $ I >= 0; $ I --) {// print space for ($ j = 0; $ j <= 5-$ I; $ j ++) {echo "& nbsp ;";} // print * for ($ k = 1; $ k <= 2 * $ I-3; $ k ++) {echo "*";} echo "<br/> "; }?>

 

4. Modify the reversed code, 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)

<? Php // $ n = 5; for ($ I = 1; $ I <= 5; $ I ++) {// print spaces for ($ j = 1; $ j <= 5-$ I; $ j ++) {echo "& nbsp;" ;}// print * for ($ k = 1; $ k <= 2 * $ I-1; $ k ++) {// print the last line after the first line and connect * ($ I = 1 | $ I = 5) if ($ I = 1) {// remove $ I = 5 and empty echo "*";} else {// question of how to empty the space and * if ($ k = 1 | $ k = 2 * $ i-1) {echo "*";} else {echo "& nbsp;" ;}}} echo "<br/>" ;}// reverse the blank for ($ I = 5; $ I >= 0; $ I --) {// print space for ($ j = 0; $ j <= 5-$ I; $ j ++) {echo "& nbsp ;";} // print * for ($ k = 1; $ k <= 2 * $ I-3; $ k ++ ){ // How to fill in the blank and * question if ($ k = 2 * $ I-3 | $ k = 1) {echo "*";} else {echo "& nbsp;" ;}} echo "<br/>" ;}?>

 

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

<? Php $ n = 5; for ($ I = 1; $ I <= $ n; $ I ++) {// print spaces for ($ j = 1; $ j <= $ n-$ I; $ j ++) {echo "& nbsp;" ;}// print * for ($ k = 1; $ k <= 2 * $ I-1; $ k ++) {// print the last line after the first line and connect * ($ I = 1 | $ I = 5) if ($ I = 1) {// remove $ I = 5 and empty echo "*";} else {// question of how to empty the space and * if ($ k = 1 | $ k = 2 * $ i-1) {echo "*";} else {echo "& nbsp;" ;}}} echo "<br/>" ;}// reverse the blank for ($ I = $ n; $ I >= 0; $ I --) {// print space for ($ j = 0; $ j <= $ n-$ I; $ j ++) {echo "& nbsp ;";} // 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 "& nbsp;" ;}} echo "<br/>" ;}?>

 

:

The final success is achieved. By the way, programming is not difficult, but it is difficult to find a solution to the problem. Therefore, everyone is good at thinking in the programming process. Although it took me half a day to think about it, I finally came up with it and solved the problem. So I usually need to practice it with my brains. Our programming capability can be improved. Only by practice can things be understood and knowledge points be mastered.

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.