siva.picky
siva.picky

Reputation: 549

How to pass string into if condition statement in php?

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";
}

Upvotes: 0

Views: 1275

Answers (2)

PrakashG
PrakashG

Reputation: 1642

You can do like this as well:

echo (25 > 1 ? "success" : ( 25 < 1 ? "success" : "failed")) ;

Upvotes: 1

Miroslav Glamuzina
Miroslav Glamuzina

Reputation: 4587

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';
}

Upvotes: 0

Related Questions