1

I have a variable $allrule = '(1 == 2) && (2 == 2)'; when i check if($allrule), it returns true because $allrule is treated as string. So i want to convert $allrule as condition of if statement. how can we do it.

6
  • Convert it to what? $allrule is a variable containing the string '(1 == 2) && (2 == 2)'. Commented Jun 9, 2015 at 14:27
  • You would have to use evil() or write some switch statements. But why do you want to do this? Commented Jun 9, 2015 at 14:28
  • Eval not evil @Rizier123 Commented Jun 9, 2015 at 14:29
  • 3
    eval IS evil, @goikiu... it should never be used... Commented Jun 9, 2015 at 14:29
  • 1
    @Goikiu I know what I'm writing... Commented Jun 9, 2015 at 14:29

1 Answer 1

3

This solution uses eval() which is pure evil, but since it wasn't stipulated not to do so...

$allrule = '(1 == 2) && (2 == 2)';
$result = eval("return (".$allrule.");"); // $result will be false

Expanded Example*:

$allrule = "(1433861812 > 1433694000) && (1433861812 > 1433771400) && (1433861812 > 1433944200)";

$result = eval("return (".$allrule.");");
if($result) {
    echo "true";
} else {
    echo "false"; // will echo "false"
}

*from comments

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

5 Comments

Provided solution is working fine but it return parse error. Parse error: syntax error, unexpected ')'
What is your real $allrule? It might have an extra parentheses.
@Darkes My real $allrule = "(1433861812 > 1433694000) && (1433861812 > 1433771400) && (1433861812 > 1433944200)";
@Darkes i modified it but it still gives me parse error; Can you provide the actual eval statement for provided string. $allrule = "(1433861812 > 1433694000) && (1433861812 > 1433771400) && (1433861812 > 1433944200)"
Hello @RanjanKumar, I've added your allrule to my answer. Your allrule parses properly. (Please pardon the delay in my responding)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.