我希望将大于5的数字再次转换为1-5,例如:
6 become 1
7 become 2
8 become 3
9 become 4因此,如果我在我的函数中输入数字6-9,它将转换为上述解释。
my_function(6); //will become 1
my_function(7); //will become 2 and so on...发布于 2011-10-07 11:57:54
function my_function( $num ) {
if ( $num % 5 === 0 ) {
return 5;
}
return $num % 5;
}模运算符%返回一个数字除以另一个数字时的余数。
发布于 2011-10-07 11:58:36
使用Modulus operator, %,它会给出除法的剩余部分。
function RangeOneToFive($num)
{
// Without the subtract and add this would range 0 to 4.
return (($num - 1) % 5) + 1;
}发布于 2011-10-07 12:05:18
function my_function( $num ) {
( $num % 5 === 0 ) ? $num = 5 : $num = $num % 5;
return $num;
}https://stackoverflow.com/questions/7682879
复制相似问题