0

Possible Duplicate:
PHP String Calculation

I am trying to make a 24-game solver.

Can i do something like this?:

$x = '*';
$a = 1;
$b = 2;

$sum = $a $x $b; //Trying something like this
$sum = $a * $b; //Actual sum

if($sum == $result) echo 'Hello World!';

Or $x as an array.

It's so confusing.

Community
  • 1
  • 1
peterdoesco.de
  • 529
  • 3
  • 19
  • 1
    fyi, a "sum" is only when you add two numbers. If you multiply two numbers, it's a "product." Or just "the result." – octern Mar 12 '12 at 01:29

2 Answers2

2

Are you sure you want to do that?

Why not:

$x = '*';
$a = 1;
$b = 2;

$sum1 = $x == '*' ? $a * b : false;
$sum2 = $a * $b;

if($sum1 == $sum2) echo 'Hello World!';

That'll have the same effect, without trying to eval a string.

fred2
  • 995
  • 1
  • 8
  • 27
0

The easiest way is to use eval which lets you execute a php string. Read the CAUTION section on that page though: do not to pass any user provided data into it.

Vyktor
  • 19,854
  • 5
  • 58
  • 94
C.Evenhuis
  • 25,310
  • 2
  • 59
  • 70