0

how can I generate numbers between 0 - 99, 100-199 .. so on the for loop? I'm trying this:

for( $i = 0 , $x = 10000 ; $i < $x ; $i += 99 ){
         echo $i , '<br />';    
}

The result is

0 
99
198
297
396
495
594
693

I Need

0 , 99 , 199 , 299 , 399 , 499 , 599 , 699
4
  • unfortunatly i can't downvote this question Commented Sep 1, 2011 at 11:07
  • @sukinsan why would you want to? Commented Sep 1, 2011 at 11:12
  • because it is super easy Commented Sep 1, 2011 at 11:20
  • I don't think you should downvote someone because their problem seems easy to you. Although it may have been a simple solution, it was obviously overlooked by the OP which happens to everyone from time to time Commented Sep 8, 2011 at 17:40

7 Answers 7

2

This is more of a math problem than a programming problem. Here's what you're looking for:

echo '0<br />';
for ($i = 100; $i < 10000; $i += 100){
    echo ($i - 1) , '<br />';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Works. I forgot it, I thought it would to do is direct in
Wouldn't this give -1 in the first iteration, while he's looking for 0?
@poplitea: I think you were looking at the old version when you posted your comment (which you are right about). :)
1

One case is special (the 0), and the others differs 100.

for( $i = -1 , $x = 10000 ; $i < $x ; $i += 100 ){
     if($i == -1){
         echo "0", "<br />";
     }else{
         echo $i , '<br />';
     }
}

1 Comment

With performance in mind (not that it really matters in this case; it's just a good habit), checking for if($i == -1) inside the loop doesn't really make sense. You might as well print out the first value and then enter the loop (so you're not performing an unneeded comparison each iteration).
1
$arr = range(-1, 10000, 100);
$arr[0] = 0;
echo implode('<br />', $arr);  

Comments

0

You'll have to do the first (99) step outside the loop, because 0 -> 99 is a stepsize of 99, and every following is a stepsize of 100.

Comments

0

This any good?

<?php

for( $i = 99 , $x = 10000 ; $i < $x ; $i += 100 ){
if ($i == 99) echo 0, '<br />';
echo $i , '<br />';    
}

?>

Comments

0

Following Brianreavis, I would just correct him with:

<?php
for($i = 0; $i <= 10000; $i += 100)
{
    $output = 0;
    if($i > 0)
        $output = ', ' . ($i - 1);
    echo $output;
}
?>

2 Comments

Not to be fresh, but: how is this a correction? You're just eating up more CPU cycles...
Please don't be. When I wrote my answer, I have apparently seen your old comment, just like poplitea did. See the comments on your own post. Your answer was originaly displaying -1 at first loop. I have also replaced the '<br />' by commas and spaces which Andrey requested initially. And I am not eating up more cycles, but agree that it has more operations per loop.
0

Why do you need the numbers? Do you want to split a set on a hundred items? In that case, it could be easier;

$i = 0;
while ( $i <= 10000 ) {
    $hundreds = floor( $i / 100 ); // 0 for 0-99, 1 for 100-199, etc
    $data[ $hundreds ][ $i ] = 'stuff';

    // array( 
    //     0 => array( 0 => 'stuff', ... 99 => 'stuff' ) ), 
    //     1 => array( 100 => 'stuff', ... 199 => 'stuff' ) )
    // )

    $i ++;
}

Doesn't directly answer your question, but might be what you're looking for.

1 Comment

Yeah, but the $i ++ was supposed to be in the // do stuff section 0:) Kidding, edited the post, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.