Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP检查特定范围内的变量_Php - Fatal编程技术网

如何使用PHP检查特定范围内的变量

如何使用PHP检查特定范围内的变量,php,Php,我必须编写一个php代码来检查从50到1000的var x,范围步长为25,这里我将用这个算法进行解释: var x if x <= 62 { then x is rounded to 50 } if x >=63 and x <= 87 { x is rounded to 75 } if x >= 88 and x <= 112 { x is rouned to 100 } var x 如果x=63,x=88,x除以25,将答案四舍五入,然后乘以25

我必须编写一个php代码来检查从50到1000的
var x
,范围步长为25,这里我将用这个算法进行解释:

var x

if x <= 62
{
  then x is rounded to 50
}
if x >=63 and x <= 87 {
  x is rounded to 75
}
if x >= 88 and x <= 112 
{
x is rouned to 100
}
var x

如果x=63,x=88,x除以25,将答案四舍五入,然后乘以25

62 / 25 = 2.48  round(2.48) = 2   2*25 = 50
63 / 25 = 2.52  round(2.52) = 3   3*25 = 75
等等

这就是你想要的:

$input = 113;
$result = round($input/25);
$result = $result * 25;
echo $result;

只需为所需范围定义一个循环(或将其作为一个函数,范围作为输入参数):



谢谢,那么范围呢?如何检查50-75之间的范围。。。975到1000?@MohamedMasmoudi如果结果是75,你期望的范围是多少?谢谢,范围是多少?如何检查50-75之间的范围。。。975到1000?或者我不会这样做?无论你把
$x
的值加进去,计算都是有效的。我的数字只是例子。请参阅@mitkosoft在其答案中输入的代码。这只是基础数学,真的。“做这件事最好的方法是什么?”-这不是你应该首先问的问题。至少先展示你自己的尝试。
<?php
    $step = 25;
    for($i=50; $i<=1000; $i++) {
        $temp = round($i / $step) * $step;
        echo $i.' is rounded to '.$temp.'<br>';
    }
?>