How can I calculate values in a string containing the following numbers and (+/-) operators:
Code Like
$string = "3+5+3-7+4-3-1";
//result should be = 4
Updated: I am trying to calculate $array = [1, +, 6, -, 43, +, 10];
I have converted into the string: implode("", $array);
you can use eval
$string = "3+5+3-7+4-3-1";
eval( '$res = (' . $string . ');' );
echo $res;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With