I got a function:
- which should add to number $x random number from -25 to 25
- if result is higher than 255 or lower than 0, the function should be called again to generate acceptable result.
What I got is:
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;
}
}
Why this:
$x = changeParam(255);
var_dump($x);
does give me null sometimes?
I've tried to check it, but I didn't find the reason:
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'
}
}