What is wrong with the output of this function, it prints out INF int the browser where as I expected it to print out 654321 The exact same function written in C# print out the expected result.
<?php
function reverse($n, $r){
if($n == 0) {
return $r;
}
return reverse($n/10, $r*10 + $n%10);
}
echo reverse(123456, 0);
?>
asked Sep 4, 2013 at 9:27
luca.p.alexandru
1,7706 gold badges24 silver badges47 bronze badges
2 Answers 2
Try the strrev() function:
strrev('123456'); //654321
h2ooooooo
39.6k8 gold badges70 silver badges108 bronze badges
answered Sep 4, 2013 at 9:29
Liam Allan
1,1151 gold badge8 silver badges13 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
It does not do integer division. In C# you deal with strict typing, and when you divide 25 by 4 in C# you will get integer result, 6. In php you will get 6.25, float result.
Try intval your results before recursion to get integer division
answered Sep 4, 2013 at 9:31
baldrs
2,1611 gold badge26 silver badges35 bronze badges
2 Comments
luca.p.alexandru
Thank you,this is the answer is expected
Deepanshu Goyal
@juice then please click on the tick mark symbol visible below the voting symbol of this answer
lang-php
strrev.reverse((int)($n/10), $r*10 + ($n%10));