简体   繁体   English

PHP:每次将数字增加5

[英]PHP: increment a number by 5 each time

I'm trying to reproduce the following as a one-liner. 我正在尝试将以下内容重现为一种。

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;

(not syntactically correct but you get the idea) (语法上不正确,但您可以理解)

So that if the number is less than 10, $next is 5, and if the number is less than 20 then it's 10. 因此,如果数字小于10,则$ next为5,如果数字小于20,则为10。

$next = ((round($l, -1)-5)); is as close as I can get to it but that gives 尽我所能接近,但这给了

5
15
25
35
45
55
65
75
85

not the desired 5, 10, 15, 20 .. etc 不是所需的5、10、15、20等

What is the correct way to write this? 写这个的正确方法是什么?

add 10 to your number, then divide the result by 10 and round it down to the nearest (floor) integer, you will then have the number by which to multiply 5, which will yield your result... so... let's say your number is 47. 将10加到您的数字上,然后将结果除以10,然后将其四舍五入为最接近的(整数)整数,然后您将得到与5乘以5的数字,这将得出结果...所以...让我们说您的电话号码是47。

47 + 10 = 57 47 + 10 = 57

57 / 10 = 5.7 57/10 = 5.7

floor 5.7 = 5 底5.7 = 5

5x5 = 25 5x5 = 25

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

(($l + 10) / 10) * 5可以解决问题

除以十(并四舍五入为整数),然后乘以5 ... 100/10 = 10 => 10 * 5 = 50

This might do: 这可能会做:

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

如果我正确理解了您的问题,那并不比难。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM