php add comma to number code example

Example 1: sum of numbers with comma php

<?php
  $input = "1,2,3,4,5,6,7,8,9,10";
    
    $sum = 0;
    
    $input_arr = explode(",", $input);
    
    print_r(array_sum($input_arr)); #with array function
    
    // without array function
    
    $sum = 0;
    foreach($input_arr as $v){
    	//print "</br>".$v;
		$sum = $sum + $v;    
    }
    print "</br>".$sum;   
?>

Example 2: number format to float php

$num = '1,200,998.255';

########## FOR FLOAT VALUES ###########################

echo filter_var($num, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);

#output  : 1200998.255

########## FOR INTEGER VALUES ###########################

echo filter_var($num, FILTER_SANITIZE_NUMBER_INT);

#output  : 1200998

Tags:

Php Example