Search code examples
phpstringinttyping

PHP int to string


I know that in PHP you can declare a variable A and then if you increment it in a loop it will go A,B,C,....X,Y,Z,AA,AB,AC

Is there any php function to convert an integer to the corresponding string in that format


Solution

  • I started with Tor P's answer and I think this works as desired:

    function numToString($i) {
        if($i < 26) {
            return chr(65 + $i);
        } else {
            $i -= 26;
            $r = $i % 26;
            return numToString($i - $r).chr(65 + $r);
        }
    }