Announcement

Collapse
No announcement yet.

PHP coding questions... How to...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP coding questions... How to...

    Does anyone know how to make PHP count? For example I need to come up with the following and I don't want to manually do it. I'm lazy...

    I need it to count from 1-6000 as follows:


    1,2,3,4,5,6,7,8,9,10,11,....5995,5996,5997,5998,5999,6000

    Thanks in advance...

  • #2
    for ( x=0;, x < 6001, x++ {

    echo ($x);

    }

    Comment


    • #3
      for ($i=0; $i<6000; print (++$i));

      Comment


      • #4
        man my code was very rusty haha.

        Comment


        • #5

          Comment


          • #6
            Originally posted by Max Albert
            for ($i=0; $i<6000; print (++$i));

            Thanks guys, this counted from 1 to 6000 but how can I add a comma after each number? I tried to add a comma everywhere in that code but all it did was return a parse error.

            Comment


            • #7
              for ($i=0; $i<6000; print (++$i . ","));

              Comment


              • #8
                don't print comma after 6,000:
                for ($i=0; $i<6000; $i++, print ($i % 6000) ? "$i, " : $i);

                Comment


                • #9
                  Damn, you guys are good! Thanks a million...

                  Comment


                  • #10
                    Only problem with your code there Max, is that it has to perform and if() and a modulus six thousand times... which isn't ideal for the server.

                    This code doesn't:
                    PHP Code:
                    $numbers = array();
                    for (
                    $i=1$i<6001$numbers[]=$i++);
                    print 
                    implode(','$numbers); 
                    Last edited by Kier; Tue 5 Mar '02, 3:18pm.

                    Comment


                    • #11
                      True. That was pretty bad on my part.

                      But, an array isn't so hot either, it takes memory. implode() adds a certain load to the server too.
                      PHP Code:
                      for ($i=0$i<5999; print (++$i ', '));
                      print (
                      '6000'); 

                      Comment


                      • #12
                        Hehe, this thread could go on for quite some time

                        Comment


                        • #13
                          PHP Code:
                          for ($i=0$i<6000; print (++$i ', '));
                          print (
                          '6000'); 

                          it has to be $i<6000 because it wont count 6000 because that woudl be equal to it .

                          Comment


                          • #14
                            It won't loop when $i==6000, BUT it's using pre-increment so when $i==5999 it prints 6000. Notice I'm also starting with $i=0, but it prints 1.

                            Comment


                            • #15
                              It's easier than that, for loop not needed:

                              PHP Code:
                              echo implode(','range(16000)); 
                              It's about 75% faster than using the for loop I believe. Built in PHP functions are good
                              Last edited by jeffct; Wed 6 Mar '02, 11:05am.

                              Comment

                              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                              Working...
                              X