-1

Possible Duplicate:
PHP: Calculate a math function f(x) in a string

I have a little problem. I have a string, such that,

$a = '(2+3+4)';

How can I handle $a such that the output, echo $a should be 9 instead of just giving (2+3+4).

Community
  • 1
  • 1
gfdfg gfgg
  • 7
  • 1
  • 3

2 Answers2

3

If your code is indeed $a = (2+3+4), then echo will output 9.

It sounds like you have it a string. You could eval() that string to get the 9.

alex
  • 479,566
  • 201
  • 878
  • 984
2

Have a look at php's Eval function

$string = "(2+3+4)";
eval('$a = '.$string);
echo $a;
Frederik Eychenié
  • 1,253
  • 7
  • 8