2

My condition is generated by the code into a string to look like:

$mycondition = "(strpos(strtolower($foo1), strtolower($bar)) !== false) AND (strpos(strtolower($foo1), strtolower($bar2)) !== false) // AND ((strpos(strtolower($foo2), strtolower($bar1)) !== false)) AND..."; ad infinitum 

My if statement should be like:

if ($mycondition) then... 

I can do it with eval() like so:

if (eval("return $mycondition;")) then... 

But I understand eval() is not recommended practice. On searching the forum, I see many voices calling against the use of eval(), but not really many alternatives.

What's the alternative? How do I just place the generated string into the if condition?

2
  • One workaround is that at the place where you are generating the condition, there only you can make the comparisons and generate a boolean at the end which you can later in the code. Commented Oct 5, 2013 at 20:03
  • Thank you @neeagl for opening my eyes to this - it solved my problem! I'm keeping a running comparison for each condition with a boolean variable which i end up checking in the if statement. Works like a charm. Thanks! No more evil eval()! Commented Oct 5, 2013 at 22:06

1 Answer 1

1

One workaround is that at the place where you are generating the condition, there only you can make the comparisons and generate a boolean at the end which you can later in the code.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.