Page 1 of 1

need help with simple for loop new to PHP and forum Read mo

Posted: Sat Feb 09, 2013 1:53 am
by ak4744710
I need to figure out how to make 5 rows of * and only use one for loop it has been driving me nuts all week any help is greatly appreciated because I have tried so many different ways. I am trying to create this but with only one for loop in php
*
**
***
****
*****

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sat Feb 09, 2013 3:36 am
by requinix
What was your best attempt? I bet it doesn't need much work to get it working correctly.

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sat Feb 09, 2013 1:02 pm
by ak4744710
for($i=0; $i<5; $i++){
echo "*";

}

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sat Feb 09, 2013 6:15 pm
by requinix
That will print exactly five stars and only once.

You need another loop that counts how many stars should be outputted per line, from one star to five stars. The loop you have now should count to that number instead of always going to five.

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sat Feb 09, 2013 6:21 pm
by Christopher
And I bet if you looked in the manual in the string functions you could find a function that would repeat a character N times.

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sun Feb 10, 2013 12:08 am
by twinedev
ak4744710 wrote:and only use one for loop
requinix wrote:You need another loop
Doesn't fit the assignments requirements (sorry, this really seems like an assignment for a programming class)

Christopher is right on about the route to take, learn the functions here: http://www.php.net/manual/en/ref.strings.php

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sun Feb 10, 2013 12:34 am
by requinix
twinedev wrote:Doesn't fit the assignments requirements
My bad. Saw the stars, thought it was a "learning how to use loops" assignment.

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sun Feb 10, 2013 10:49 am
by Christopher
requinix wrote:
twinedev wrote:Doesn't fit the assignments requirements
My bad. Saw the stars, thought it was a "learning how to use loops" assignment.
Uh oh ... requinix is seeing stars and acting loopy! ;)

Re: need help with simple for loop new to PHP and forum Rea

Posted: Sun Feb 10, 2013 12:46 pm
by ak4744710
I got it

Re: need help with simple for loop new to PHP and forum Rea

Posted: Tue Feb 12, 2013 7:27 am
by darkbox
Can you post the codes? I just want to compare it to my work.

Thanks