Alex Alex - 2 months ago 4x
PHP Question

Add commas in amount using php

Please i have a jquery code to add commas in number at the right place now i don't want to use jquery for it again how do i do this using php to add commas?
bellow is my attempt

The jquery

<script type="text/javascript">
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");

var bal_cc = $(this).html();
bal_cc = numberWithCommas(bal_cc);





PHP Tried


function numberWithCommas(x) {
return x.str_replace(/\B(?=(?:\d{3})+(?!\d))/g, ",");
$bal = numberWithCommas($UserCradit);
echo $ubal;


This is wrong: x.str_replace(/\B(?=(?:\d{3})+(?!\d))/g, ","); you should learn to use Php Manual before coming to stack overflow. number_format( is what you are looking for

    $bal = number_format($UserCradit);
    echo $bal;