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
  • 1,015
  • 2
  • 9
  • 29
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
  • 20,559
  • 6
  • 64
  • 96
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72