search

Home  >  Q&A  >  body text

PHP outputs the entire pyramid exercise

<?php
$n = 10;
for($i = 1; $i <= $n; $i ++) {
    for($k = 1; $k <= $n - $i; $k ++) {
        echo " ";
    }
    for($j = 1; $j <= ($i - 1) * 2 + 1; $j ++) {
        echo '*';
    }
    echo '<br />';
}
?>

Please tell me why the following effect is displayed:

I have checked many times and cannot find the reason. Please give me some advice. Thank you!

大家讲道理大家讲道理2806 days ago647

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-16 13:03:08

    Mine is the whole thing. What did you test it on?

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:03:08

    The logic of your code is correct, but the use of   in the display takes up two ** characters, so it does not look like a pyramid. This may be related to the software problem you are debugging.

    reply
    0
  • 为情所困

    为情所困2017-05-16 13:03:08

    After inserting the codeecho "&nbsp;"; 改写成 echo "&ensp;";, test it in each browser to display the entire pyramid.
    Thank you teachers for your guidance, thank you very much!

    reply
    0
  • Cancelreply