Actually, it's slightly easier than that in PHP:

$num = number_format($num,0);
which will turn 1234567 into 1,234,567. You can also specify a character to use instead of a comma, and the amount of decimal places you want, e.g.
$foo = 123;
$foo = number_format($foo,2);
which will give 123.00.