Home >Backend Development >PHP Tutorial >Pyramid graphics implemented using loops in PHP, php loop pyramid graphics_PHP tutorial
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:
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:
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.