Php star triangle

Solved
Hello,
I want to create a triangle using the character * with a loop based on a number given in a variable $size (example with 5 here)

*
**
***
****
*****
Configuration: Windows XP Internet Explorer 6.0

15 answers

  1. Désolé, je suis uniquement ici pour traduire.
    0
    1. ah yes, shit, I forgot that one...
      a mistake of intention

      mark as resolved, bye.
      0
      1. oh thank you :) it was the br
        0
        1. it gives me that: ***************
          I don't understand anything anymore
          0
          1. Oops... forgot the line break every time...

            size = 5; for ($i = 1; $i <= $size; $i++) { for ($k = 1; $k <= $i; $k++) { echo "*"; } echo '
            '; }
            0
            1. Hello

              I doubt it will work much better :)

              If your inner loop uses the same variable as the outer loop, your outer loop is useless
              If both go up to $taille, you will always draw 5 stars....

              $taille = 5; for ($i = 1; $i <= $taille; $i++) { for ($k = 1; $k <= $i; $k++) { echo "*"; } }
              0
              1. well in that case it only shows me this

                *****
                0
                1. I told you not to forget the { and }, if it's not in your code that's normal

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


                  so the logic isn't really there ....

                  - your loops from 1 to 5 because you have 1 star and you want 5 and not 0 to 4
                  - don't forget the braces otherwise your loops are useless
                  - make sure to place your echo '*'; correctly
                  0
                  1. I tried that but it doesn't work

                    $size = 5;
                    for ($i = 0; $i < $size; $i++)
                    echo "*". "
                    ";
                    for ($i = 0; $i < $size; $i++)
                    echo "*";
                    0
                    1. ```php <?php echo("sat cv"); $a=10; for($i=0;$i<$a;$i++) { for($j=0;$j<$i;$j++) { echo("*"); } echo "
                      "; } ?> ```
                      0
                  2. You forgot a loop

                    The first loop will be used to create the 5 lines 5lines -> 5stars

                    The second loop will create the correct number of stars

                    Basically, you redo the same loop, but this time you will put your character '*',

                    without forgetting the {} around each loop ^^
                    0
                    1. $size = 5;
                      for ($i = 0; $i < $size; $i++)
                      echo "* ";

                      I'm stuck here
                      0
                      1. ```php <?php

                        $l=5;
                        $es=$l-1;
                        $et=1;

                        for($i=0 ; $i<$l ; $i++)
                        {
                        for($k=1 ; $k<=$es ; $k++)
                        {
                        echo "";
                        }

                        for($b=1 ; $b<=$et ; $b++)
                        {

                        echo "*";
                        }
                        echo "<br/>";
                        $es--;
                        $et+=2;

                        } ```
                        0
                    2. Euh no, it'll give you an error

                      you can't multiply an int variable with a string variable ^^'

                      it would be useful though
                      0
                      1. we have the right to do
                        nb = number of times
                        star = *

                        echo nb X "star"
                        0
                        1. Well, I don't see where you're getting stuck, apart from the fact that I don't see you using it afterwards, but I'm not judging. However, you mentioned a loop.

                          If you thought about that, I don't see what your problem is. The implementation is quite simple.

                          - You define a variable size (number of stars) if you want the generation to be dynamic (controlled by a form, for example).

                          - You create a loop from 1 to $size that increments by 1 each time (for the rows).

                          - You place $size number of stars on each line based on $size, so you will have to create another loop from 1 to $size again for the number of stars (echo '*';)

                          And in the end, you will have your triangle.

                          That's the basic idea (a very, very French draft algorithm, lol).

                          Now it's up to you to continue.
                          0