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.

5
  • From the docs: ...the passed code must be valid PHP.... for eval(): php.net/manual/en/function.eval.php Commented May 10, 2018 at 19:17
  • 1
    You should add return and ;: $compare="5<6";echo eval("return $compare;"); Commented May 10, 2018 at 19:18
  • 2
    Before using eval you should have read this question: stackoverflow.com/questions/951373/when-is-eval-evil-in-php and at least it's top 3 answers. Commented May 10, 2018 at 19:19
  • echoing true will display 1 and echoing false will not display anything. Commented May 10, 2018 at 19:21
  • it is reading from another file so will only evaluate code that I have written Commented May 10, 2018 at 19:43

4 Answers 4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

it's reading from another file, so it's only reading code that I'm writing into it
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!
0

Try this:

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

1 Comment

Don't forget to sanitise the string before using eval.
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 ;)

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.