So I have been experimenting with some single complex lines of code and ran across this:
[php]function roman_numerals($num){
$c = ‘IVXLCDM’;
$num = return_whole_number ($num);
for($a = 5, $b = $s = ''; $num; $b++, $a^=7) {
for ($o = $num % $a, $num = $num / $a ^ 0; $o--; @$s = $c[$o > 2 ? $b + $num - ($num &= -2) + $o = 1 : $b] . $s) ;
}
return $s;
}[/php]
The original code did not have the suppression added to it, I added them to hide an error that was being generated, although I am not so much worried about the error right now, I am more interested in the concept of this code… I find this fascinating.
Let me give an example, if I call:
[php]echo roman_numerals(2178);[/php] it’s output would be, as expected MMCLXXVIII
OK, I am finding this pretty cool, but now the question is WHY?
The first for loop, why is $a starting at 5? it look like it is going through a loop of 5 and 2. Am I wrong on this?
Then the second loop… Ok, $o is the number being assigned, now it takes the number dividing it by the XOR 5 or 2 then giving a power of 0? Yeah, this has got me on that… Can anyone explain how this works?
Thank you