• support@answerspoint.com

How to create Pyramid of asterisks in php

419

I need to create a pyramid using asterisks. I specify a value which becomes the base of the pyramid. I am facing a problem. Please see my code.

<?php
    $n = $i = 5;
    while ($i--)
    echo str_repeat(' ', $i).str_repeat('*', $n - $i)."\n";
?>

 

  • PHP

  • asked 9 months ago
  • Priyanka Roy

2Answer


0
<html>
    <head>
    <title>create Pyramid of asterisks in php</title>
    </head>
<body>
<pre>
<?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
?>
</pre>
</body>
</html>

 

Text in a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks.

  • answered 9 months ago
  • Community  wiki

0

use This program within <center> </center> tag

<center>
<?php

    for($i=1;$i<=5;$i++){
        for($j=1;$j<=$i;$j++){
                    echo "*";
        }
        echo "<br />";
    }

?>
</center>

 

  • answered 9 months ago
  • Community  wiki

Your Answer

    Facebook Share