How can I create a pyramid from using php?

前端 未结 14 1190
耶瑟儿~
耶瑟儿~ 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:41
    $n = 5;
    for($i = $n; $i >= 1; $i--){
        for($j = 1; $j <= $i; $j++){
            if($j < $i){
                echo '0';//you can replace 0 with space
            }else{
                $num = $n - $i;
                for($k = 0; $k <= $num; $k++){
                    echo '*';
                }
                for($y = 1; $y <= $n - 1; $y++){
                    if($y <= $num){
                        echo '*';
                    }else{
                        echo '0';//you can replace 0 with space
                        if($y == $n - 1){
                            echo '<br/>';
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-11 09:42
    function create_piramide ($filas) {    
        $n = $filas;
        echo '<pre>';
        for ($i = $filas; $i >= 0; $i--) {              
            echo str_repeat(' ', $i).str_repeat('o ', $n - $i)."\n";
        }
        echo '</pre>';
    } 
    
    create_piramide (10);
    
    0 讨论(0)
提交回复
热议问题