Half PHP increase

I have a quick question that is probably easy to answer. I collapsed, but not sure if I'm looking correctly or what. Anyway, using PHP, how can I increase half?

For example, I know that I can use the following loop:

<?php 
for ($i=1; $i<21; $i++) {
    print($i);
}

And he will print 1 - 20.

But how can I make it output something like the following:

1
1.5
2
2.5
etc...

Sorry for my ignorance of this, I just don't know how to do this. Thank!

+5
source share
5 answers

Change $i++to $i += 0.5. In addition, to print each number on its own line, you need to use \n(or <br>, if you output HTML to a browser).

for ($i = 1; $i < 21; $i += 0.5) {
    print($i . "\n");
}

20.5, 21. 20, , $i <= 20 :

for ($i = 1; $i <= 20; $i += 0.5) {
    print($i . "\n");
}
+22

.

foreach (range(1, 20, 0.5) as $i) {
  // Do something with $i
}
+8

Loop to double the sum (adjust the upper and lower bounds accordingly) and divide by two at the output.

eg.

for ($i=2; $i<41; $i++) print($i/2);

to output from 1 to 20 in increments of .5

+2
source

instead $i++, use$i += .5

+1
source

Something might work here.

$i += round(exp(log(2)/2) * 2) / 2 - ENT_QUOTES + IMAGETYPE_JPEG;
0
source

All Articles