Skip to content
Advertisement

How to use condition operator pulled from database and added to script with PHP

I am storing my condition operators in the database as we have a bunch of different conditions and it needs to be inserted in the code dynamically. So, I am pulling the condition operator from the database and then storing it in the variable. But then I need actually use the operator in my if statement. How can in insert it into if statement and use it as regular condition operator?

Here is some example of code:

JavaScript

Advertisement

Answer

The only way you can do what you show is with eval(). But it’s generally frowned upon to use eval() because it introduces lots of potential security vulnerabilities.

I would do this hard-coded, to avoid eval() and make sure I can control the specific operators I want to support:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement