How can I create a pyramid from using php?

前端 未结 14 1249
耶瑟儿~
耶瑟儿~ 2020-12-11 08:58

I need to create a pyramid using asterisks. I specify a value which becomes the base of the pyramid. The base contains as much asterisks as the value specified and the pyram

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-11 09:31

    Just make it simpler:

    function create_pyramid($limit) {
        for($row = 1; $row < $limit; $row ++) {
            $stars = str_repeat('*', ($row - 1) * 2 + 1);
            $space = str_repeat(' ', $limit - $row);
            echo $space . $stars . '
    '; } } echo "
    " ;
    create_pyramid(10);
    

提交回复
热议问题