1

Given something like this:

$comparison = '=='; // it could also be '>', '<', etc
$val1 = 5;
$val2 = 10;

How can I use $comparison as the comparison?
ie. Instead of:

if ($val1 == $val2)

Doing something like:

if ($val1 $comparison $val2)

Is eval the only option here?

Jason Varga
  • 1,949
  • 2
  • 21
  • 29

1 Answers1

0

something like this?

function compare($comparison, $val1, $val2) {
    switch($comparison) {
        case '==':
            return $val1 == $val2;
            break;
        // etc
    }
}

if (compare('==', 1, 4))
OtotheA
  • 513
  • 4
  • 12