0
this is what i want.
123456
 23456
  3456
   456
    56
     6

Hi, i have trouble with this loop.

    <?php
    for ($x = 7; $x >= 1; $x--) {
        for ($y = 7; $y > $x; $y--) {
            echo "&nbsp;&nbsp";
        }
        $s = 7;
        while ($s < $x) {

            $f++;
            $s--;
        }

        for ($f=1; $f < 7; $f++) {
            echo "$f";
        }
        echo "<br>";
    }
    ?>

this is what i got. I want to get the $f work but it is ignoring it.

4
  • a better question would include your desired output Commented Nov 6, 2016 at 22:04
  • i dont think you need 4 loops Commented Nov 6, 2016 at 22:06
  • Why don't you just take the starting string and remove the first character from the string? Commented Nov 6, 2016 at 22:07
  • I see 2 dimensions, right and down, yet I count 4 loops. Can be done with just 2, just saying.. Commented Nov 6, 2016 at 22:14

1 Answer 1

1

You can make it simpler than you did.

for($x = 1; $x <= 6; $x++) {

    for($y = 1; $y <=6; $y++){ 
        if($x > $y)
            echo "&nbsp&nbsp;";
        else
            echo $y;
    }
    echo "<br>";
}

With x you control the lines and with y the columns. If the lines is greater than the column you print the spaces, and if not, the number.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.