I need to make a 3-level Christmas first step with a cycle, starting from an input where you are asked to enter a number from 1 to 3. (Without the spaces)
So far I have been able to print it but in this way:
This is the current code:
<?php
echo "<center>";
for ($i=1; $i<=3; $i++){
for ($k=1; $k<=3; $k++)
{
for ($j=1; $j<=$k; $j++)
{
echo "*";
}
echo "<br>";
}
}
echo "***<br>";
echo "***";
echo "<center/>";
?>
Well you can save yourself a
for
usestr_repeat
and well I leave you the code:What I did is simple, first I modified your
for
to initialize them at 0 (something that perhaps was not necessary) but it helps me to be able to do the following calculation: as you can see I make a sum withi + k + 1
because this helps to control that in the first iteration it starts from 1 (* ) an asterisk and then in the second it starts with (**) two and so on, this is put as a parameter instr_repeat
which the only thing it does is repeat the string of (*) so you don't have to do anotherfor
.Result:
I hope it helps you. Cheers ;)
This is one way to do it and where you can define the number of lines of your Christmas tree.
This example calculates the spaces, inserts the characters "*" and at the end inserts a line break
<br>
:Departure: