|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Mar 2005
Posts: 217
|
Write a FOR loop in PHP that prints the numbers 0-9 to the page.
How would i go about this?
|
|
|
|
|
|
#2 |
|
SQL Consultant
![]() ![]() ![]() ![]() Join Date: Jul 2002
Location: Toronto, Canada
Posts: 32,859
|
homework assignment?
what have you tried so far?
__________________
r937.com | rudy.ca | Buy my SitePoint book: Simply SQL "giving out my real stuffs"
|
|
|
|
|
|
#3 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Mar 2005
Posts: 217
|
Not really. Saw this question on a skills test. I have no idea where to start.
I first inclination is to put the numerals 1-9 in array and wrote a for loop to simply print all of the array contents? That would be too easy. There's gotta be a way to handle numbers programmatically. Can you point me in the right direction? Last edited by bruin03; Nov 7, 2008 at 09:38.. |
|
|
|
|
|
#4 |
|
¬.¬ shoooo...
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2005
Location: CA
Posts: 7,387
|
|
|
|
|
|
|
#5 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2006
Location: GA
Posts: 2,254
|
Code:
foreach(range(0,9) as $num) {
echo $num;
}
|
|
|
|
|
|
#6 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,047
|
In a for loop as requested:-
PHP Code:
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
#7 |
|
SitePoint Mentor
![]() ![]() ![]() Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
|
Would have been good to let him have a stab at it first, noone learns from copying and pasting!
|
|
|
|
|
|
#8 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,047
|
Possibly, but as a solution had already been posted I thought it best he at least gets what he asked for.
Saying that, I usually find it easier to learn something given an example, it's all good and well trying to cobble a solution together, but if you don't even know where to start... It's nearly Christmas too, festive giving and all that! ![]()
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
#9 |
|
SitePoint Mentor
![]() ![]() ![]() Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
|
Nah, normally I would let people have a stab at it first, post something up, then help them with that. Better than just giving away the answer straight off
![]() But yeh, since an answer had already been posted, may as well post the one he wanted! |
|
|
|
|
|
#10 |
|
Community Advisor
![]() ![]() Join Date: Jun 2004
Location: Copenhagen, Denmark
Posts: 6,067
|
If it doesn't have to specifically be a for-loop, you could do:
PHP Code:
__________________
Introducing pearhub | Konstrukt 2.3.1 | twitter.com/troelskn |
|
|
|
|
|
#11 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Dec 2005
Posts: 966
|
or even
PHP Code:
__________________
MySQL v4.1.18 PHP v5.2.4 |
|
|
|
|
|
#12 |
|
dooby dooby doo
![]() ![]() ![]() Join Date: Aug 2004
Location: Manchester UK
Posts: 11,822
|
OK, if we really want to get down and dirty....
PHP Code:
__________________
Community Team Leader Only a woman can read between the lines of a one word answer..... Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator |
|
|
|
|
|
#13 |
|
SitePoint Mentor
![]() ![]() ![]() Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
|
Ah, why not confuse things further then :P
PHP Code:
PHP Code:
Code:
<++++++++[>++++++<-]>>++++++++++[<.+>-] |
|
|
|
|
|
#15 |
|
SitePoint Mentor
![]() ![]() ![]() Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
|
PHP Code:
PHP Code:
|
|
|
|
|
|
#16 |
|
dooby dooby doo
![]() ![]() ![]() Join Date: Aug 2004
Location: Manchester UK
Posts: 11,822
|
LMAO, If bruin03 doesn't get an A* then its a fix!
__________________
Community Team Leader Only a woman can read between the lines of a one word answer..... Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator |
|
|
|
|
|
#17 |
|
SitePoint Mentor
![]() ![]() ![]() Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
|
Now I'm spending all my time trying to make obscure solutions to this problem...
|
|
|
|
|
|
#18 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,047
|
wow, good show StormRider.
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() |
|
|
|
|
|
#19 |
|
Twitter: @AnthonySterling
![]() Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,047
|
Another...
PHP Code:
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja. Also future, drunk, attendee of the PHPNW10 Conference - come say hello! ![]() Last edited by AnthonySterling; Nov 5, 2008 at 03:27.. Reason: This one actually works! :-S |
|
|
|
|
|
#20 |
|
SitePoint Enthusiast
![]() Join Date: Oct 2008
Location: Pakistan
Posts: 67
|
<?php
for($i=0; $i<=9; $i++) { echo $i; } ?> |
|
|
|
|
|
#21 |
|
Zend Certified Engineer
![]() Join Date: Oct 2006
Location: Kathmandu, Nepal
Posts: 3,629
|
I wonder why this thread got that much replies. Is it only because the problem was quite easy or it really needs that much examples to print 0 to 9 numbers in a print using for loop in PHP?
![]() |
|
|
|
|
|
#22 |
|
SitePoint Zealot
![]() ![]() Join Date: Jul 2008
Posts: 188
|
PHP Code:
|
|
|
|
|
|
#23 |
|
SitePoint Addict
![]() ![]() ![]() Join Date: Jan 2002
Location: Southwest Florida
Posts: 392
|
What a great thread! It's interesting to see how many different ways there are to do even a very simple thing in php.
PHP Code:
|
|
|
|
|
|
#24 | |
|
SitePoint Mentor
![]() ![]() ![]() Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
|
Quote:
![]() PHP Code:
|
|
|
|
|
|
|
#25 |
|
SitePoint Mentor
![]() ![]() ![]() Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
|
PHP Code:
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 10:02.





















Linear Mode
