转换大于5的数字

okn*_*rap 0 php numbers

我希望转换数大于5再次变为1-5,例如:

6 become 1
7 become 2
8 become 3
9 become 4
Run Code Online (Sandbox Code Playgroud)

所以,如果我输入数字6-9到我的函数,它将转换为上面的解释.

my_function(6); //will become 1
my_function(7); //will become 2 and so on...
Run Code Online (Sandbox Code Playgroud)

Wil*_*ill 6

function my_function( $num ) {
    if ( $num % 5 === 0 ) {
         return 5;
    }
    return $num % 5;
}
Run Code Online (Sandbox Code Playgroud)

%当一个数除以另一个数时,模数运算符返回余数.


Joh*_*ter 6

使用模数运算符,%它给出了除法的余数.

function RangeOneToFive($num)
{
   // Without the subtract and add this would range 0 to 4.
   return (($num - 1) % 5) + 1;
}
Run Code Online (Sandbox Code Playgroud)