php conditional statement from string variable -


i have question on php if statements. lets have situation:

$id = 1; $condition = "$id > 0"; if($condition){...} 

how can parse $condition variable actual condition like:

$id = 1; $condition = "$id > 0"; if($id > 0){...} 

is there simple , secure way this?

you have use function called eval. evaluates string text php code. documentation

if want this, @ least read warning

caution eval() language construct dangerous because allows execution of arbitrary php code. use discouraged. if have verified there no other option use construct, pay special attention not pass user provided data without validating beforehand.

something should work:

$id = 1; $condition = eval("$id > 0"); if($condition){...} 

edit:

eval expects complete line of code, need use this:

$id = -1; $condition = eval('return $id > 0;'); if ($condition) {     echo 'true'; } else {     echo 'false'; } 

Comments

Popular posts from this blog

javascript - How to update a get parameter in a url with jquery -

c - Run time Error with scanf -

amazon web services - How to determine the state of an AWS instance -