mogleng mogleng - 1 month ago 18
PHP Question

Asterisk pyramid using php while loop

$i=0; $j=5; $k=0;

while($i < 5 ) {
while($j > $i){
echo " ";
$j--;
}
while($k <= $i){
echo '*';
++$k;
}

echo "<br/>";
$i++;
}


I am trying to creating asterisk pyramid using while loop with php, but the output as image enclosed. The asterisk character not show an pyramid. Instead of just single line:

output from above code

Please enlighten me the error of the code.

Answer
<pre><?php

$left  = 4;
$right = 4;
$i = 0;

while($i < 5 ) {
     $j=0;
     while($j<11){
        if($j >= $left  &&  $j <= $right){
               echo "*";
         }else{
                 echo " ";
          }
        $j++;
    }

 echo "<br>";
 $left--;$right++;
 $i++;

} ?>

try this one, If you are interested to use only while loop, But try to understand the flow and learn and debug your issues