0

I have stored conditions in database string format. ex: ((25>1)||(25<1)) How can I pass this string to if condition statement. I tried, but it's not working. I have used eval() function, but it's not working.

$con=eval("return ((25>1)||(25<1))");
if($con){
    echo "success";
}
else{
    echo "failed";
}
9
  • 3
    Parse error: syntax error, unexpected end of file, expecting ';' : eval()'d code on line 1 Commented Apr 11, 2019 at 14:32
  • 1
    $con=eval("return ((25>1)||(25<1));"); sandbox.onlinephpfunctions.com/code/… Commented Apr 11, 2019 at 14:34
  • @AbraCadaver sorry i forgot to add semicolon inside of eval <?php $con=eval("return ((25>1)||(25<1));"); if($con){ echo "success"; } else{ echo "failed"; } ?> Commented Apr 11, 2019 at 14:37
  • @AbraCadaver my doubt instead of eval any alternative is their? Commented Apr 11, 2019 at 14:39
  • 1
    OK, then decode it. You keep commenting things that should be in your question. Commented Apr 11, 2019 at 14:59

2 Answers 2

1

You can do like this as well:

echo (25 > 1 ? "success" : ( 25 < 1 ? "success" : "failed")) ;
Sign up to request clarification or add additional context in comments.

1 Comment

this conditional statement okay. but my question is how to convert string ex: ((25>1)||(25<1)) this directly pass to if condition, i tried now eval worked but most of say eval is not safe so i try finding any other alternative, do you know something alternative for this?
0

You are missing a semicolon in your eval(). Strings passed into eval() still must be valid PHP, which will need an ;.

$con = eval('return ((25>1)||(25<1));');
if($con) {
    echo 'success';
} else {
    echo 'failed';
}

5 Comments

This working thanks!. is that any alternate for eval?
Why would you need an alternative?
because i searched most of say don't use eval .
I've found some different ways to do it from this post, is this what you are looking for? stackoverflow.com/a/17700065/5066625
Using eval isn´t a good thing, but as long as there is no (un-validated) user input, it´s fine security wise.

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.