views:

168

answers:

5

An easy question to ask, hope not to stupid.

$var=0;
$condition="$var!=0";
if($condition) {
    # do something #
}
else {
    # do something else #
}

Obviously the code up there doesn't work as intended. Is there a nice way to obtain an if-condition from a string? Or do I have to parse the string in some disturbing way?

EDIT I didn't explain myself very well. The fact is that the string could contain any possible condition you can immagine es:

  • $var > 0
  • $var < 0
  • $var == 0
  • etc

I read this condition from an xml file as a string, so I don't know what I will find.

+3  A: 

The eval() function is the easy way out here. E.g.:

if(eval($condition)) {
    # do something #
}
else {
    # do something else #
}

It must be stressed that eval() is evil. It is very easy to make a website extremely insecure using eval(), even if you really know what you're doing. In particular, even if you're sure the code is safe now, it might not be so after you add a few features in totally different parts of the code.

That said eval() has its uses, particularly in quick-and-dirty single use scripts like migration scripts.

If you're writing a world-facing website, you need to take _extreme_ care if using eval(). Very often, the necessary amount of care is more difficult than implementing some non-eval() solution.

Artelius
You were the first. ***GLARE***. `eval` is evil! If you've found a problem where `eval` is the solution, `eval` is not the solution!
Charles
You said it yourself: `eval` is evil. Sometimes it's the least evil option, in which case it is the appropriate one. Just because a tool is really easy to misuse doesn't mean it should never be used.
Artelius
@Charles I have to agree. Though I understand that he is just providing a possible solution so you can't hate completely. But I really would have to wonder what circumstance someone would find themselves in where you would have to use an eval. Especially for a seemingly easy circumstance.
spinon
I used a PHP-based CMS named Ariadne, which has user-editable pieces of code, in a restricted PHP language. It pre-processed the code to insert `$this->` in front of every function call except those on the approved list, similarly with variables, then `eval`-ed it. Of course there were gotchas - it's obvious you don't put `eval` on the approved list, but you better not put `preg_replace` on it either since it's `eval` in disguise. However, it was a good hack, and did what it did surprisingly well. It doesn't change the fact that `eval` is Evil, just goes to show even Evil does good sometimes.
Amadan
It really depends on the circumstance. In a single-use script that you will use to, e.g. reformat some of your data files, `eval()` can save you a good deal of time. On an internet-facing website it should almost always be avoided.
Artelius
Who are those stupids who upvoted this?
Col. Shrapnel
A: 
  1. The contents of your $condition are not $var!=0, but 0=0 (due to $ not being escaped (\$) within double quotes.

  2. You can do this with eval function.

  3. However, eval is Evil. Ask yourself if you really need to do this. If there is any other way, take it.

Amadan
+1  A: 

why not just do:

$var = 0;
if ($var != 0){
    # do something #
}
else {
    # do something else #
}

EDIT: Or even

$var = 0;
$condition = 0
if ($var != $condition){
    # do something #
}
else {
    # do something else #
}
spinon
This sort of misses the point of the question.
Dolph
He edited the question after I had already made this post. Now with the edit it does.
spinon
A: 

OK, you could use evil Eval. But if your condition is actually as simple as you propose, you could just use a regex to test the condition... No eval necessary.

jpp
+4  A: 

Just because no one has posted it yet. Ask yourself what comparisons need to be made. If they're doing simple math operations you might want to have something in the schema like this.

<conditions>
  <notEqual var="var" value="0" />
</conditions>

It'll allow you to have multiple conditions, should be relatively simple to parse and convert to php code.

AlReece45
Exactly what I was saying. +1.
Amadan
Uhm nice. Thinking about it I've only 6 possible comparisons. Thank you. +1
Abaco
@Amadan didn't see your comment before. You might have wanted to put it in an answer (it was still a little different)
AlReece45
@Abaco Thanks! Surprized me by accepting it. I just didn't want to see this post close with only one non-eval "Answer" available
AlReece45
@AlReece45: Don't worry about it ;)
Amadan