简体   繁体   中英

Function not working as expected in PHP

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);
?>

尝试strrev()函数:

strrev('123456'); //654321

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM