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";
}
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
siva.picky
  • 499
  • 5
  • 17

2 Answers2

1

You can do like this as well:

echo (25 > 1 ? "success" : ( 25 < 1 ? "success" : "failed")) ;
PrakashG
  • 1,642
  • 5
  • 20
  • 30
  • 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? – siva.picky Apr 11 '19 at 15:05
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';
}
Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33