3

If I have:

$compare = "5<6";

How do I look at the compare variable and return the value true. The input needs to be a string so taking away the " won't help. The string could be very complex so I'm looking for a function already in PHP which can run this, or something that someone has written before which can do this. I've tried the eval() function but that doesn't work, so:

$compare = "5<6";
echo eval($compare);

just errors. Thanks for your help in advance.

Syscall
  • 19,327
  • 10
  • 37
  • 52

4 Answers4

0

Try this:

<?php 
$compare = "5<6";
eval('$output = '.$compare.';');
echo $output;
?>
0

Using code evaluation functions are something that should be discouraged, because it may lead to a lot of security problems, but if you want to keep your logic that way, you must evaluate a valid code:

<?php
  $compare = "5 > 6";
  eval("\$result = $compare;");

  var_dump($result);

Your comparing stringmust be used inside the eval function like it would be done in the normal code, not expecting a return value. So what I changed in this sample code is that I used a variable named $result to store the value from the evaluated code.

As you will see when running this code, the $result will be a boolean value (false in this sample, because 5 is not greater than 6, obviously).

Luan
  • 157
  • 3
  • 7
  • it's reading from another file, so it's only reading code that I'm writing into it – Codey Chase May 10 '18 at 19:41
  • 2
    I'm just warning because you should avoid using this kind of function (in any environment/project, not just PHP). Of course you can use this kind of code, but you shouldn't :P Just be aware of the risks and (maybe) research a little bit about it, to see what are the problems that it will may lead (for future projects, perhaps). Anyway... hope it helped you! – Luan May 10 '18 at 19:46
0

From PHP documentation: http://php.net/manual/en/function.eval.php

Caution The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

You can display with echo directly:

<?php
$compare = '5<6';
echo $compare. "\n";
echo eval("return $compare;");
?>

Or You can store result in variable to display later:

<?php
$compare = '5<6';
echo $compare. "\n";
$result = eval("return $compare;");
echo $result;
?>

Be careful ;)

Yeti82
  • 383
  • 1
  • 6
  • 14
0

If you wanted to avoid eval it's easy enough to parse comparison expressions. Just split the string on a comparison operator and return the result of the corresponding comparison.

function compare($expr) {
    $expr = preg_split('/(>=|<=|<|>|=)/', $expr, -1, PREG_SPLIT_DELIM_CAPTURE);
    list($lhs, $op, $rhs) = $expr;
    switch ($op) {
        case '<':  return $lhs <  $rhs;
        case '<=': return $lhs <= $rhs;
        case '>':  return $lhs >  $rhs;
        case '>=': return $lhs >= $rhs;
        case '==': return $lhs == $rhs;
    }
}

Apparently I missed the bit about "the string could be very complex" when I read the question initially, so this may not be helpful in your case, but I'll leave it here for future reference.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80