<?php
function calculate($a, $b, $op) {
//change commas to periods
$a = str_replace(',', '.', $a);
$b = str_replace(',', '.', $b);
//determine which arithmetic operation to use
switch ($op) {
case '+': $ret = $a + $b; break;
case '-': $ret = $a - $b; break;
case '*': $ret = $a * $b; break;
case '/': $ret = $a / $b; break;
case '%': $ret = fmod($a, $b); break;
default: return false;
}
//change periods back to commas and return
return str_replace('.', ',', $ret);
}
//calculate 18,55 * 1,25
echo calculate("18,55", "1,25", "*");
?>
Basically, number_format() isnt needed.
All that's being done here is treating the comma as the decimal point.