Pyramid chart implemented by loop in PHP

Source: Internet
Author: User
This article mainly introduces the pyramid chart implemented by loops in PHP. This article is a PHP class note, which summarizes the instructor's homework, for more information, see the basic PHP conditions and loop statements learned today. Finally, the teacher asked a few questions, one of which is more fun to implement the hollow pyramid.

What the teacher wants us to achieve is:

*

**

.

First:

The code is as follows:

For ($ I = 1; $ I <= 5; $ I ++ ){
Switch ($ I ){
Case 1:
Echo ""."*"."";
Break;
Case 2:
Echo "";
Break;
Case 3:
Echo ""."*".""."*"."";
Break;
Case 4:
Echo "";
Break;
Case 5:
Echo "*".""."*".""."*";
Break;
}
Echo"
";

The implementation effect is the image requested by the teacher, but the teacher thinks there should be a simpler implementation method, which can separate the space and the * number;

So there is the second type:

The code is as follows:


For ($ a = 0; $ a <4; $ a ++ ){
For ($ B = 4; $ B> $ a; $ B --){
Echo "";
}
For ($ c = 1; $ c <= $ a; $ c ++ ){
Echo "*";
}
For ($ d = 0; $ d <= $ a; $ d ++ ){
Echo "*";
}
Echo"
";
}

The effect of this implementation is not as hollow as it was just now. The effect is as follows:

*
***
*****
*******

This is not implemented yet. I will try again later. Today, the first blog is completed, and I hope it can be written for a long time.

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.