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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ak4744710
Forum Newbie
Posts: 5
Joined: Sat Feb 09, 2013 1:50 am

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

Post 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
*
**
***
****
*****
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

What was your best attempt? I bet it doesn't need much work to get it working correctly.
ak4744710
Forum Newbie
Posts: 5
Joined: Sat Feb 09, 2013 1:50 am

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

Post by ak4744710 »

for($i=0; $i<5; $i++){
echo "*";

}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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! ;)
(#10850)
ak4744710
Forum Newbie
Posts: 5
Joined: Sat Feb 09, 2013 1:50 am

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

Post by ak4744710 »

I got it
darkbox
Forum Newbie
Posts: 3
Joined: Tue Feb 12, 2013 6:56 am

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

Post by darkbox »

Can you post the codes? I just want to compare it to my work.

Thanks
Post Reply