Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 3, 2008, 15:13   #1
bruin03
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?
bruin03 is offline   Reply With Quote
Old Nov 3, 2008, 15:14   #2
r937
SQL Consultant
silver trophybronze trophy
SitePoint Award Recipient
 
r937's Avatar
 
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"
r937 is online now   Reply With Quote
Old Nov 3, 2008, 15:25   #3
bruin03
SitePoint Addict
 
Join Date: Mar 2005
Posts: 217
Quote:
Originally Posted by r937 View Post
homework assignment?

what have you tried so far?
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..
bruin03 is offline   Reply With Quote
Old Nov 3, 2008, 15:27   #4
logic_earth
¬.¬ shoooo...
SitePoint Award Recipient
 
logic_earth's Avatar
 
Join Date: Oct 2005
Location: CA
Posts: 7,387
http://us2.php.net/manual/en/control-structures.for.php
__________________
Logic without the fatal effects.
All code snippets are licensed under WTFPL.

logic_earth is online now   Reply With Quote
Old Nov 3, 2008, 15:57   #5
oddz
SitePoint Wizard
 
Join Date: Jul 2006
Location: GA
Posts: 2,254
Code:
foreach(range(0,9) as $num) {
  echo $num;
}
http://us3.php.net/manual/en/function.range.php
oddz is offline   Reply With Quote
Old Nov 4, 2008, 00:38   #6
AnthonySterling
Twitter: @AnthonySterling
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,047
In a for loop as requested:-

PHP Code:

<?php
for( $iCounter = 0 ; $iCounter <= 9 ; $iCounter++ )
{
    echo
$iCounter . '<br />';
}
?>
__________________
@AnthonySterling: Full-time IT Manager, part-time Freelance PHP Ninja.
Also future, drunk, attendee of the PHPNW10 Conference - come say hello!
AnthonySterling is offline   Reply With Quote
Old Nov 4, 2008, 01:21   #7
Stormrider
SitePoint Mentor
silver trophybronze trophy
 
Stormrider's Avatar
 
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!
__________________

Stormrider is offline   Reply With Quote
Old Nov 4, 2008, 01:44   #8
AnthonySterling
Twitter: @AnthonySterling
 
AnthonySterling's Avatar
 
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!
AnthonySterling is offline   Reply With Quote
Old Nov 4, 2008, 01:49   #9
Stormrider
SitePoint Mentor
silver trophybronze trophy
 
Stormrider's Avatar
 
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!
__________________

Stormrider is offline   Reply With Quote
Old Nov 4, 2008, 03:22   #10
kyberfabrikken
Community Advisor
silver trophy
 
kyberfabrikken's Avatar
 
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:

echo implode(",", range(0,9)); 

kyberfabrikken is offline   Reply With Quote
Old Nov 4, 2008, 07:57   #11
BrandonK
SitePoint Guru
 
Join Date: Dec 2005
Posts: 966
or even
PHP Code:

echo '1 2 3 4 5 6 7 8 9'; 

__________________
MySQL v4.1.18
PHP v5.2.4
BrandonK is offline   Reply With Quote
Old Nov 4, 2008, 08:32   #12
spikeZ
dooby dooby doo
silver trophybronze trophy
 
spikeZ's Avatar
 
Join Date: Aug 2004
Location: Manchester UK
Posts: 11,822
OK, if we really want to get down and dirty....
PHP Code:

for($i=0; $i<=1; $i++) {
    echo
'the numbers 0-9';
}
should answer the question exactly......
__________________
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
spikeZ is online now   Reply With Quote
Old Nov 4, 2008, 08:37   #13
Stormrider
SitePoint Mentor
silver trophybronze trophy
 
Stormrider's Avatar
 
Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
Ah, why not confuse things further then :P

PHP Code:

for ($i=0;$i<=9;print $i++); 

PHP Code:

for($i=48;$i<=57;print(chr($i++))); 

Have a version in brainf*ck as well:

Code:
<++++++++[>++++++<-]>>++++++++++[<.+>-]
__________________

Stormrider is offline   Reply With Quote
Old Nov 4, 2008, 15:44   #14
Ernie1
play of mind
 
Ernie1's Avatar
 
Join Date: Sep 2005
Posts: 1,199
PHP Code:

$x = 0;


while (
$x < 10)
{
    echo
$x++;
}
__________________
my mobile portal
ghiris.ro
Ernie1 is offline   Reply With Quote
Old Nov 5, 2008, 01:50   #15
Stormrider
SitePoint Mentor
silver trophybronze trophy
 
Stormrider's Avatar
 
Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
PHP Code:

$x = 10;
while (
$x) echo 10 - ($x--);
PHP Code:

for ($d1=0;$d1<2;$d1++)
for (
$d2=0;$d2<2;$d2++)
  for (
$d3=0;$d3<2;$d3++)
   for (
$d4=0;$d4<2;$d4++) {
       echo
base_convert($d1.$d2.$d3.$d4,2,10);
    if (
$d1.$d2.$d3.$d4 == "1001") break 4;
   }
//for
__________________

Stormrider is offline   Reply With Quote
Old Nov 5, 2008, 02:22   #16
spikeZ
dooby dooby doo
silver trophybronze trophy
 
spikeZ's Avatar
 
Join Date: Aug 2004
Location: Manchester UK
Posts: 11,822
Quote:
Originally Posted by Stormrider View Post
PHP Code:

$x = 10;

while (
$x) echo 10 - ($x--);
PHP Code:

for ($d1=0;$d1<2;$d1++)

for (
$d2=0;$d2<2;$d2++)
  for (
$d3=0;$d3<2;$d3++)
   for (
$d4=0;$d4<2;$d4++) {
       echo
base_convert($d1.$d2.$d3.$d4,2,10);
    if (
$d1.$d2.$d3.$d4 == "1001") break 4;
   }
//for
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
spikeZ is online now   Reply With Quote
Old Nov 5, 2008, 02:27   #17
Stormrider
SitePoint Mentor
silver trophybronze trophy
 
Stormrider's Avatar
 
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...
__________________

Stormrider is offline   Reply With Quote
Old Nov 5, 2008, 02:49   #18
AnthonySterling
Twitter: @AnthonySterling
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,047
Quote:
Originally Posted by Stormrider View Post
PHP Code:

$x = 10;
while (
$x) echo 10 - ($x--);
PHP Code:

for ($d1=0;$d1<2;$d1++)
for (
$d2=0;$d2<2;$d2++)
  for (
$d3=0;$d3<2;$d3++)
   for (
$d4=0;$d4<2;$d4++) {
       echo
base_convert($d1.$d2.$d3.$d4,2,10);
    if (
$d1.$d2.$d3.$d4 == "1001") break 4;
   }
//for
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!
AnthonySterling is offline   Reply With Quote
Old Nov 5, 2008, 03:11   #19
AnthonySterling
Twitter: @AnthonySterling
 
AnthonySterling's Avatar
 
Join Date: Apr 2008
Location: North-East, UK.
Posts: 4,047
Another...

PHP Code:

<?php
$aRange
= array('M','N','O','A','Q','g','w');
for (
$i=0;$i<=6;$i++)
{
    for (
$j=0;$j<=6;$j++)
    {
        
$k = base64_decode($aRange[$i].$aRange[$j].'==');
        if(
is_numeric($k))
        {
            echo (
$l != $k) ? $k . '<br />' : null ;
            
$l = $k;
        }
    }
}
?>
__________________
@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
AnthonySterling is offline   Reply With Quote
Old Nov 5, 2008, 03:21   #20
asady
SitePoint Enthusiast
 
Join Date: Oct 2008
Location: Pakistan
Posts: 67
Smile

<?php
for($i=0; $i<=9; $i++)
{
echo $i;
}
?>
asady is offline   Reply With Quote
Old Nov 5, 2008, 03:27   #21
rajug
Zend Certified Engineer
bronze trophy
 
rajug's Avatar
 
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?
rajug is offline   Reply With Quote
Old Nov 5, 2008, 03:52   #22
Limotek
SitePoint Zealot
 
Join Date: Jul 2008
Posts: 188
PHP Code:

$loopy = array("0. Don't", "1. be", "2. such", "3. a", "4. spoilt", "5. sport!", "6. Let", "7. them", "8. enjoy", "9. it!");


function
test_print($item2)
{
    echo
substr($item2,0,1);
}

array_walk($loopy, test_print);
Limotek is offline   Reply With Quote
Old Nov 5, 2008, 05:00   #23
sonjay
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:

$n = '';
for(
$i = 0; $i<=9; $i++) {
    echo
strlen($n);
    
$n = $i.$n;
}
sonjay is offline   Reply With Quote
Old Nov 5, 2008, 05:20   #24
Stormrider
SitePoint Mentor
silver trophybronze trophy
 
Stormrider's Avatar
 
Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
Quote:
Originally Posted by sonjay View Post
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:

$n = '';
for(
$i = 0; $i<=9; $i++) {
    echo
strlen($n);
    
$n = $i.$n;
}
Nice idea

PHP Code:

for($i=0;$i<9;print strlen($n.=$i++)); 

__________________

Stormrider is offline   Reply With Quote
Old Nov 5, 2008, 05:29   #25
Stormrider
SitePoint Mentor
silver trophybronze trophy
 
Stormrider's Avatar
 
Join Date: Sep 2006
Location: Nottingham, UK
Posts: 2,721
PHP Code:

for($i=print(0);$i<=9;print($i++)); 

__________________

Stormrider is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 10:02.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved