Pyramid graphics _php techniques for using loops in PHP

Source: Internet
Author: User

Today I learned the most basic PHP conditions and looping statements, the last teacher raised a few questions, one of the implementation of the hollow pyramid image is more fun.

What the teacher wants us to achieve is:

*

* *

* * The Hollow pyramid.

First type:

Copy Code code 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 "<br>";

The implementation of the effect is the teacher requested the image, but the teacher felt that there should be a more simple way to achieve, you can separate the space and the * number of output;

And then there's the second kind:

Copy Code code 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 "<br>";
}

The effect of this implementation is not just the middle hollow, the effect is as follows:

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

This has not achieved hollow, will try again, today's first blog is completed, I hope that the future can be long written.

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.