Say I have a string, $char. $char == "*".
$char. $char == "*".
I also have two variables, $a and $b, which equal "4" and "5" respectively.
How do I get the result of $a $char $b, ie 4 * 5 ?
$a $char $b, ie 4 * 5 ?
Thanks :)
You can use eval() as suggested by @konforce, however the safest route would be something like:
eval()
$left = (int)$a; $right = (int)$b; $result = 0; switch($char){ case "*": $result = $left * $right; break; case "+"; $result = $left + $right; break; // etc }
2.1m questions
2.1m answers
60 comments
57.0k users