Im trying to compute a simple expression in PHP, the expression will always be the same the only change is in the operator.
Is there a simple solution to rather than duplicate the formula, just use the single expression and have the operator as a variable.
Something like..
function calc($qty, $qty_price, $max_qty, $operator_value, $operator = '-')
{
$operators = array(
'+' => '+',
'-' => '-',
'*' => '*',
);
//adjust the qty if max is too large
$fmq = ($max_qty > $qty)? $qty : $max_qty ;
return ( ($qty_price . $operators[$operator] . $operator_value) * $fmq ) + ($qty - $fmq) * $qty_price;
}
eval()but that's usually a bad code smell