harsh sharma harsh sharma - 1 month ago 9
PHP Question

Not getting output as expected in printing star pattern in php

I have tried this code and output i am getting is here

<?php
for($i=1;$i<=5;$i++){
for($j=5-$i;$j>=1;$j--){
echo "&nbsp;";
echo "&nbsp;";
}
for($k=1;$k<=$i;$k++){
echo $i;
}
echo "<br/>";
}
?>
1
22
333
4444
55555


but i want output like this, please tell where i am wrong

1
12
123
1234
12345

Answer

Just change echo $i to echo $k,

for($k=1;$k<=$i;$k++){
    echo $k;
}