PHP增量增加一半 [英] PHP Increment by Half

查看:78
本文介绍了PHP增量增加一半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速的问题,可能很容易回答.我已经四处搜寻,但是不确定是否正确搜索或搜索了什么.无论如何,使用PHP,我怎么能减半?

I have a quick question, which is probably easy to answer. I've goolged around, but not sure if I am searching correctly or what. Anyway, using PHP, how can I increment by halves?

例如,我知道我可以使用以下循环:

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

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

它将打印1-20.

但是,如何获取类似于以下内容的输出:

But, how can I get it to output something like the following:

1
1.5
2
2.5
etc...

很抱歉,我对此一无所知,但我不确定该如何处理.谢谢!

Sorry for my ignorance on this, I'm just not sure how to go about it. Thanks!

推荐答案

$i++更改为$i += 0.5.另外,要在每个行上打印每个数字,您需要使用\n(如果要将HTML输出到浏览器,则使用<br>).

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");
}

上面的代码将打印20.5,因为它小于21.如果要打印最大值20,请更改循环条件以改为选择$i <= 20:

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");
}

这篇关于PHP增量增加一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆