PHP Increment by Half

浪子不回头ぞ 提交于 2019-12-04 03:11:22

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

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

The above code will print 20.5 because it's less than 21. If you want to print a maximum of 20, change the loop condition to check $i <= 20 instead:

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

Just one more solution to choose from.

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

Loop to double the amount (adjust upper and lower bounds appropriately) and divide by two in the output.

E.g.

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

to output from 1 to 20 in increments of .5

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

Here's something that could work.

$i += round(exp(log(2)/2) * 2) / 2 - ENT_QUOTES + IMAGETYPE_JPEG;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!