PHP:每次增加一个数字5


PHP: increment a number by 5 each time

我正试图将以下内容作为一行代码进行复制。

if($l < 10) $next = 5; return;
if($l < 20) $next = 10; return;
if($l < 30) $next = 15; return;
if($l < 40) $next = 20; return;
if($l < 50) $next = 25; return;
if($l < 60) $next = 30; return;
if($l < 70) $next = 35; return;
if($l < 80) $next = 40; return;
if($l < 90) $next = 45; return;
if($l < 100) $next = 50; return;

(语法不正确,但你明白了)

因此,如果数字小于10,$next是5,如果数字少于20,则是10。

$next = ((round($l, -1)-5));尽我所能接近它,但这给出了

5
15
25
35
45
55
65
75
85

不是所需的5、10、15、20。。etc

写这篇文章的正确方式是什么?

将10加到你的数字上,然后将结果除以10,四舍五入到最接近的(floor)整数,然后你就得到了与5相乘的数字,这将产生你的结果。。。所以假设你的号码是47。

47+10=57

57/10=5.7

地板5.7=5

5x5=25

return floor(($i + 10)/10) * 5

(($l + 10) / 10) * 5将完成

DIVIDE通过十(四舍五入为int),然后乘以5。。。100/10=10=>10*5=50

这可能会:

$i=10;
while($l < $i){
    $next = $i / 2;
    $i+=10;
}

如果我正确理解你的问题,它并不难;

$next = floor($l/10)*5+5;