Home >Backend Development >PHP Tutorial >Pyramid graphics implemented using loops in PHP, php loop pyramid graphics_PHP tutorial

Pyramid graphics implemented using loops in PHP, php loop pyramid graphics_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:14:411228browse

Pyramid graphics implemented using loops in PHP, php loop pyramid graphics

Today I learned the most basic PHP conditions and loop statements. Finally, the teacher asked a few questions. One of them was the image of a hollow pyramid that was quite interesting.

What the teacher wants us to achieve is:

*

* *

* * * The hollow pyramid.

First type:

Copy code 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 output spaces and * signs separately;

So there is the second kind:

Copy code 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 no longer hollow in the middle. The effect is as follows:

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

This is not hollow yet, I will try again later. Today’s first blog is completed, I hope I can write more in the future.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/908178.htmlTechArticlePyramid graphics implemented using loops in PHP, php loop pyramid graphics Today I learned the most basic PHP conditions and loop statements , finally the teacher asked a few questions, one of which realized hollow gold...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn