0

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
}
user2430227
  • 303
  • 1
  • 3
  • 13
  • 3
    Eval is generally called unsafe: http://stackoverflow.com/questions/951373/when-is-eval-evil-in-php Why can't you just create an if-else list with the $unlistedIfStatement? – Tim Feb 27 '14 at 22:09
  • 1
    That is a really suboptimal way of structuring that code. Not at all a valid use of `eval`. This would also be the wrong way to structure similar JavaScript code. You shouldn't be relying on `eval` for this. – user229044 Feb 27 '14 at 22:10
  • Yeah, I wasn't planning on keeping it this way, there's many other ways of going about this, I was just curious as to how I could store an entire line of code into a variable and have it executed in php – user2430227 Feb 27 '14 at 22:11

2 Answers2

2

The documentation that you linked to is very clear:

eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned.

Each of your eval'd lines must return a value, or they evaluate to null:

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
}
user229044
  • 232,980
  • 40
  • 330
  • 338
1

As indicated in another answer, eval() returns null in absence of a return value. However, what you're actually looking for is a callback:

Somewhere in your code:

function showUnlisted() {
  return !$price || $car["sale_price"] == 0 || ($priceFrom <= $car["sale_price"] && $priceTo >= $car["sale_price"]);
}

function dontShowUnlisted() {
  return !$price ||($priceFrom <= $car["sale_price"] && $priceTo >= $car["sale_price"]);
}

Then, where you need to decide between these functions:

if ($showUnlisted) {
  $appropriateFunction = 'showUnlisted';
} else {
  $appropriateFunction = 'dontShowUnlisted';
}

if (call_user_func($appropriateFunction)) {
  //do stuff
}

This prevents you from falling prey to the evils of eval, lets you test those functions, utilize IDEs more effectively, and predict the outcome better. Passing functions as objects is a useful thing, and while awkward in PHP, a common practice in more modern languages (Scala, C#, etc.).

Community
  • 1
  • 1
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
  • This is a really cool method and a much better approach that I didn't even think of, I just used a lazy ifelse, this is a very good answer and very useful especially in the future, thank you! – user2430227 Feb 27 '14 at 23:02
  • This is exactly what I meant by storing code in variable in JavaScript like when you do `var helloWord = function(){ alert('hola!');}`, exactly what I was looking for. – user2430227 Feb 27 '14 at 23:04