Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric operation using string

Tags:

php

I have following string.

$option = '+';
$value = '100';

I want create new value using above parameters.

ex:- $newValue = 222 + 100;

How can i do it using above parameters? as following

$newValue = 222 $option $value; 
like image 609
Dinuka Thilanga Avatar asked Oct 25 '11 10:10

Dinuka Thilanga


1 Answers

function operate($a, $b, $opt){
    switch ($opt) {
        case '+':
            $ret = $a + $b;
            break;
        case '-':
            $ret = $a - $b;
            break;
        case '*':
            $ret = $a * $b;
            break;
        case '/':
            $ret = $a / $b;
            break;
        default:
            throw new Exception('Unsupported operation!');
    }
    return $ret;
}

Edit

$option = '+';
echo operate(222,100,$option);
like image 160
xdazz Avatar answered Oct 13 '22 00:10

xdazz