Home > Backend Development > PHP Tutorial > PHP中使用循环实现的金字塔图形_PHP

PHP中使用循环实现的金字塔图形_PHP

WBOY
Release: 2016-05-31 19:28:13
Original
954 people have browsed it

今天学习了最基础的PHP条件和循环语句,最后老师提了几个问题,其中一个实现空心金字塔的图像比较好玩。

老师要让我们实现的是:

     *

    * *

   * * *  的空心金字塔。

第一种:

代码如下:

for ($i=1;$i         switch ($i){
            case 1:
                echo "  "."*"."  ";
                break;
            case 2:
                echo "     ";
                break;
            case 3:
                echo " "."*"." "."*"." ";
                break;
            case 4:
                echo "     ";
                break;
            case 5:
                echo "*"." "."*"." "."*";
                break; 
        }
    echo "
";

实现效果就是老师要求的图像,不过老师觉得应该还有更简单的实现方式,可以将空格和*号分开输出;

于是就有了第二种:

代码如下:


for($a=0;$a         for($b=4;$b>$a;$b--){
            echo " ";
        }
        for($c=1;$c             echo "*";
        }
        for($d=0;$d             echo "*";
        }
        echo "
";
    }

这个实现的效果就没有刚才那种中间空心的了,效果如下:

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

这个还没有实现空心,待会再试试,今天第一篇博客算完成了,希望以后可以长写。

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template