PHP:简单数学函数返回null而不是整数

PHP:简单数学函数返回null而不是整数,php,function,Php,Function,我得到了一个函数: -它应该加上$x随机数,从-25到25 -如果结果大于255或小于0,则应再次调用该函数以生成可接受的结果 我得到的是: function changeParam($base_val) { $plus = mt_rand(-25, 25); if (($base_val + $plus) > 255 || ($base_val + $plus) < 0) { changeParam($base_val); //is i

我得到了一个函数:
-它应该加上$x随机数,从-25到25
-如果结果大于255或小于0,则应再次调用该函数以生成可接受的结果

我得到的是:

function changeParam($base_val)
{

    $plus = mt_rand(-25, 25);

    if (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        changeParam($base_val); //is it correct?
    } 
    else 
    {   
        $new_val = $base_val + $plus;
        return $new_val;        
    }   

}
有时会给我空值吗?

我试过检查,但没有找到原因:

function changeParam($base_val)
{

    $plus = mt_rand(-25, 25);

    if (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        changeParam($base_val);
    } 
    else 
    {   
        $new_val = $base_val + $plus;
        echo 'Is there a problem? ' . $new_val;   // this line shows correct new_val
        var_dump($new_val);                       // correct, for instance: 'int 250'
        return $new_val;                           // but in the same time, the result = 'null'
    }   

}
函数changeParam($base\u val)
{
$plus=百万兰特(-25,25);
如果($base_val+$plus)>255($base_val+$plus)<0)
{
changeParam($base_val);
} 
其他的
{   
$new_val=$base_val+$plus;
echo“有问题吗?”。$new\u val;//此行显示正确的new\u val
var_dump($new_val);//正确,例如:“int250”
返回$new_val;//但同时,结果='null'
}   
}

如果你真的想使用递归,你必须在调用
changeParam($base\u val)之前添加一个
return
。所以看起来是这样的:

function changeParam($base_val)
{

    $plus = mt_rand(-25, 25);

    if (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        return changeParam($base_val); // CHANGED HERE
    } 
    else 
    {   
        $new_val = $base_val + $plus;
        return $new_val;        
    }   

}
function changeParam($base_val)
{
    $plus = mt_rand(-25, 25);

    while (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        $plus = mt_rand(-25, 25);
    } 

    $new_val = $base_val + $plus;
    return $new_val;        
}
函数changeParam($base\u val)
{
$plus=百万兰特(-25,25);
如果($base_val+$plus)>255($base_val+$plus)<0)
{
return changeParam($base_val);//在此处更改
} 
其他的
{   
$new_val=$base_val+$plus;
返回$new_val;
}   
}
然而,在这种情况下,递归可能是不好的。(如果搜索随机变量多次返回“坏”值,可能会遇到堆栈溢出异常。这种情况下发生的概率很低,但您应该总是考虑这样的情况。) 相反,您应该使用迭代方法,例如:

function changeParam($base_val)
{

    $plus = mt_rand(-25, 25);

    if (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        return changeParam($base_val); // CHANGED HERE
    } 
    else 
    {   
        $new_val = $base_val + $plus;
        return $new_val;        
    }   

}
function changeParam($base_val)
{
    $plus = mt_rand(-25, 25);

    while (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        $plus = mt_rand(-25, 25);
    } 

    $new_val = $base_val + $plus;
    return $new_val;        
}
函数changeParam($base\u val)
{
$plus=百万兰特(-25,25);
而(($base_val+$plus)>255($base_val+$plus)<0)
{
$plus=百万兰特(-25,25);
} 
$new_val=$base_val+$plus;
返回$new_val;
}

如果你真的想使用递归,你必须在调用
changeParam($base\u val)之前添加一个
return
。所以看起来是这样的:

function changeParam($base_val)
{

    $plus = mt_rand(-25, 25);

    if (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        return changeParam($base_val); // CHANGED HERE
    } 
    else 
    {   
        $new_val = $base_val + $plus;
        return $new_val;        
    }   

}
function changeParam($base_val)
{
    $plus = mt_rand(-25, 25);

    while (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        $plus = mt_rand(-25, 25);
    } 

    $new_val = $base_val + $plus;
    return $new_val;        
}
函数changeParam($base\u val)
{
$plus=百万兰特(-25,25);
如果($base_val+$plus)>255($base_val+$plus)<0)
{
return changeParam($base_val);//在此处更改
} 
其他的
{   
$new_val=$base_val+$plus;
返回$new_val;
}   
}
然而,在这种情况下,递归可能是不好的。(如果搜索随机变量多次返回“坏”值,可能会遇到堆栈溢出异常。这种情况下发生的概率很低,但您应该总是考虑这样的情况。) 相反,您应该使用迭代方法,例如:

function changeParam($base_val)
{

    $plus = mt_rand(-25, 25);

    if (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        return changeParam($base_val); // CHANGED HERE
    } 
    else 
    {   
        $new_val = $base_val + $plus;
        return $new_val;        
    }   

}
function changeParam($base_val)
{
    $plus = mt_rand(-25, 25);

    while (($base_val + $plus) > 255 || ($base_val + $plus) < 0) 
    {
        $plus = mt_rand(-25, 25);
    } 

    $new_val = $base_val + $plus;
    return $new_val;        
}
函数changeParam($base\u val)
{
$plus=百万兰特(-25,25);
而(($base_val+$plus)>255($base_val+$plus)<0)
{
$plus=百万兰特(-25,25);
} 
$new_val=$base_val+$plus;
返回$new_val;
}
函数changeParam($base\u val)
{
$plus=百万兰特(-25,25);
如果($base_val+$plus)>255($base_val+$plus)<0)
{
$new\u val=changeParam($base\u val);
} 
其他的
{   
$new_val=$base_val+$plus;
}   
//回到这里
返回$new_val;
}
函数changeParam($base\u val)
{
$plus=百万兰特(-25,25);
如果($base_val+$plus)>255($base_val+$plus)<0)
{
$new\u val=changeParam($base\u val);
} 
其他的
{   
$new_val=$base_val+$plus;
}   
//回到这里
返回$new_val;
}

谢谢@Hauke。我不知道在再次调用函数之前需要返回。因为“while”是一个更干净的解决方案,所以我会用它。再次感谢!谢谢你,豪克。我不知道在再次调用函数之前需要返回。因为“while”是一个更干净的解决方案,所以我会用它。再次感谢!