tags:

views:

102

answers:

3

I want to generate a list of numbers with a 0.25 difference between them, e.g

0
0.25
0.50
0.75
1
1.25
.....
9.75
10

How can this be done?

+4  A: 

Just use a for() loop.

for($i=0;$i<=10;$i+=0.25)
    echo $i,'<br />';
Chris Bartow
Have you tested this?
Click Upvote
Or you could : for($i=0;$i<=10/0.25;$i++) {echo $i*0.25;}
Aviral Dasgupta
Yes, tested in 5.x, but I'm guessing it will work in earlier versions.
Chris Bartow
Yep, working :)
Click Upvote
I've also tested this in PHP 5, if someone could test in 4 that'd be appreciated
Click Upvote
This is just a `for` loop. It'll certainly work in 4.
brianreavis
+12  A: 

Use the range() function. You can specify a step (0.25):

$numbers = range(0, 10, 0.25);
foreach ($numbers as $number) {
  echo "$number\n";
}
cletus
Severity: WarningMessage: Wrong parameter count for range()
Click Upvote
@Click Upvote: it seems as though the third parameter was added in PHP5 so you may be getting that message from PHP4. If that's the case, though, well... I'd argue you need to upgrade :)
Narcissus
I don't think so, you can supply additional arguments and you don't get warnings, but if you /miss/ any arguments you then get a warning.
Click Upvote
+1  A: 
$numbers = range(0, 10*4);
$l = count($numbers);    
for($i=0; $i<$l;$i++){
    $numbers[$i]/=4;
}