How to make php expressions and execute from database?

I have a database table that stores various types of statements and values โ€‹โ€‹that express expressions such as

if(mydBVal1 mydBExpression mydBval2) { // do something....! } 

Here is my code that shows both an example of what I want to say and the help I need for example:

 $data['myValue'] = 100; $data['operator'] = "<"; $data['comparison_value'] = 150 if( $data['myValue'] . $data['operator'] . $data['comparison_value'] ) { ///do something...... } 

I want the condition to be read as if(100 < 150){} , but the if condition expression is not working properly!

Does anyone know how I can make it work?

0
source share
2 answers

I think you want to use the eval() function.

Be very careful to sanitize data from the database before guessing it, although you can allow users to execute PHP code that you do not need.

 $data['myValue']=100; $data['operator']="<"; $data['comparison_value']= 150; $eval = sprintf("return(%d %s %d);", $data['myValue'], $data['operator'], $data['comparison_value']); if(eval($eval)) { 
+2
source

You can also look into php assert

php.net/assert

 <?php var_dump(assert("1 == 1")); var_dump(assert("1 === null")); ?> 

Sample code that I used related to my project:

 $assert_statement = (($typecriteria != 'IS_NULL' || $typecriteria != 'NOT_NULL' ) ? "'".$value."'" : '' ) . " " . $typecriteria . " '" . $criteriavalue."'"; // Active assert and make it quiet assert_options(ASSERT_ACTIVE, 1); assert_options(ASSERT_WARNING, 0); assert_options(ASSERT_BAIL, 0); assert_options(ASSERT_QUIET_EVAL, 1); if ( $debug >= 1 ) { print __METHOD__." assert debug ".$assert_statement."<br>"; var_dump(assert( $assert_statement )); } if (assert( $assert_statement ) === true ) { return true; } 
0
source

All Articles