SitePoint Sponsor

User Tag List

Page 1 of 5 12345 LastLast
Results 1 to 25 of 114

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Write a FOR loop in PHP that prints the numbers 0-9 to the page.

    How would i go about this?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    homework assignment?

    what have you tried so far?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 11:38.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, you guys have been great!

    Once again Sitepoint has come though. i learned a lot from this thread. Thanks!

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bruin03 View Post
    Ah, you guys have been great!

    Once again Sitepoint has come though. i learned a lot from this thread. Thanks!
    Ah ha! The instigator returns.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Code:
    foreach(range(0,9) as $num) {
      echo $num;
    }
    http://us3.php.net/manual/en/function.range.php

  8. #8
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    Code:
    foreach(range(0,9) as $num) {
      echo $num;
    }
    http://us3.php.net/manual/en/function.range.php
    I like that, sweet.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In a for loop as requested:-

    PHP Code:
    <?php
    for( $iCounter $iCounter <= $iCounter++ )
    {
        echo 
    $iCounter '<br />';
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Would have been good to let him have a stab at it first, noone learns from copying and pasting!

  11. #11
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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!

  13. #13
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it doesn't have to specifically be a for-loop, you could do:
    PHP Code:
    echo implode(","range(0,9)); 

  14. #14
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or even
    PHP Code:
    echo '1 2 3 4 5 6 7 8 9'
    MySQL v5.1.58
    PHP v5.3.6

  15. #15
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple solution.... No logic involved
    Quote Originally Posted by BrandonK View Post
    or even
    PHP Code:
    echo '1 2 3 4 5 6 7 8 9'

  16. #16
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    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......
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  17. #17
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    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......
    hahahaha I actually didnt get it at first.

    never knew about the range() function either.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  18. #18
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by halfasleeps View Post
    hahahaha I actually didnt get it at first.

    never knew about the range() function either.
    Me either!
    It's pretty neat.

  19. #19
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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:
    <++++++++[>++++++<-]>>++++++++++[<.+>-]

  20. #20
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $x 0;

    while (
    $x 10)
    {
        echo 
    $x++;

    my mobile portal
    ghiris.ro

  21. #21
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 

  22. #22
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    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!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  23. #23
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  24. #24
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Now I'm spending all my time trying to make obscure solutions to this problem...

  25. #25
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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;
            }
        }
    }
    ?>
    Last edited by AnthonySterling; Nov 5, 2008 at 05:27. Reason: This one actually works! :-S
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •