For loop in PHP
how can I generate numbers between 0 - 99, 100-199 .. so on the for loop? I'm trying t开发者_StackOverflow中文版his:
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
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 />';
}
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 />';
}
}
$arr = range(-1, 10000, 100);
$arr[0] = 0;
echo implode('<br />', $arr);
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.
This any good?
<?php
for( $i = 99 , $x = 10000 ; $i < $x ; $i += 100 ){
if ($i == 99) echo 0, '<br />';
echo $i , '<br />';
}
?>
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;
}
?>
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.
精彩评论