如何在PHP中为数字添加逗号(会话值)

I would like to change this:

4000

to this

4,000

I am using this code

<?php echo number_format(($_SESSION["result"]),",")  ; ?>

But its printing nothing...

Have a look at the parameters shown here.

<?php echo number_format(($_SESSION["result"]), 3,",", "."); ?>

Use money_format or number_format

Example 01

$number = 1234.56;

//setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "
";
//output -  USD 1,234.56

Example 02

 // number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," );
number_format($number, 0, ".", ",");
//output -  1,234.56

Try this It will give you the rupees in Indian format

function makecomma($input)
{
if(strlen($input)<=2)
{ return $input; }
$length=substr($input,0,strlen($input)-2);
$formatted_input = makecomma($length).",".substr($input,-2);
return $formatted_input;
}
function formatInIndianStyle($num){
$pos = strpos((string)$num, ".");
if ($pos === false) { $decimalpart="00";}
else { $decimalpart= substr($num, $pos+1, 2); $num = substr($num,0,$pos); }

if(strlen($num)>3 & strlen($num) <= 12){
            $last3digits = substr($num, -3 );
            $numexceptlastdigits = substr($num, 0, -3 );
            $formatted = makecomma($numexceptlastdigits);
            $stringtoreturn = $formatted.",".$last3digits.".".$decimalpart ;
}elseif(strlen($num)<=3){
            $stringtoreturn = $num.".".$decimalpart ;
}elseif(strlen($num)>12){
            $stringtoreturn = number_format($num, 2);
}

if(substr($stringtoreturn,0,2)=="-,"){$stringtoreturn = "-".substr($stringtoreturn,2 );}
$stringtoreturn=str_replace(".00","",$stringtoreturn);
return $stringtoreturn;

}

//You simply call this
echo formatInIndianStyle("4000");