Not sure how to use the eval()
I thought I read the documentation correctly and am trying to do this (I know the logic is awful, but I was just trying it out).
if($showUnlisted){
$unlistedIfStatement = '!$price || $car["sale_price"] == 0 ||($priceFrom <= $car["sale_price"] && $priceTo >= $car["sale_price"])';
}
else{
$unlistedIfStatement = '!$price ||($priceFrom <= $car["sale_price"] && $priceTo >= $car["sale_price"])';
}
if(eval($unlistedIfStatement)){
//DO SOME STUFF
}
I get this error:
Parse error: syntax error, unexpected end of file
Looking at these examples(http://us1.php.net/eval) it should run whatever is in the string as a line of code. Also this definition "The eval() function evaluates a string as PHP code." from http://www.w3schools.com/php/func_misc_eval.asp Basically, I'm trying to store a line of code in a variable and have it run like I would do in JavaScript. Very new to PHP, I'm sorry if my question is dumb.
EDIT:
I've read the many posts as to why the eval()
is a bad tool, I honestly am just asking out of curiosity.
EDIT2:
meagar's code worked splendidly, the only thing I had to do to get it to work was add semi-colons before the string ended. I went to edit his post, but my edit was denied. Here is the code that worked for me....Obviously I'll be changing it to something that my peers will not poke fun at.
if($showUnlisted){
$unlistedIfStatement = 'return !$price || $car["sale_price"] == 0 ||($priceFrom <= $car["sale_price"] && $priceTo >= $car["sale_price"]);';
}
else{
$unlistedIfStatement = 'return !$price ||($priceFrom <= $car["sale_price"] && $priceTo >= $car["sale_price"]);';
}
if(eval($unlistedIfStatement)){
//DO SOME STUFF
}