Harea Costicla Harea Costicla - 19 days ago 4
PHP Question

Divide a number recursive

I have this code :

public function divideNbPlay($nb_play){
if($nb_play % 400 !== 0){
$nb_play = divideNbPlay($nb_play % 400);
}
return $nb_play;
}


I want to get the rest of division, for example if I have

nb_play = 590, I want to return 190
nb_play = 806, I want to return 6
....

Answer

No need to use divideNbPlay function inside the function. Modulus function return remainder of divided.

function divideNbPlay($nb_play){
    $nb_play = $nb_play % 400;
    return $nb_play;
}

$nb_play = 806;
echo divideNbPlay($nb_play);