如何制作php表达式并从数据库执行? [英] how to make php expressions and execute from database?
问题描述
我有一个数据库表,其中存储了各种类型的运算符和值,这些运算符和值可以像这样的表达式
I have a database table that stores various type of operators and values that make expressions like
if(mydBVal1 mydBExpression mydBval2)
{
// do something....!
}
这是我的代码,它显示了我想说的内容以及我需要的帮助的示例例如:
Here is my code thats shows and example of what I want to say and the help I require e.g:
$data['myValue'] = 100;
$data['operator'] = "<";
$data['comparison_value'] = 150
if( $data['myValue'] . $data['operator'] . $data['comparison_value'] )
{
///do something......
}
我希望 if 条件被读取为 if(100 < 150){}
,但是 if 条件表达式不能正常工作!
I want that if condition to be read as if(100 < 150){}
, but the if condition expression is not working properly!
这里有人知道我如何让它工作吗?
any one here know how I can make it work?
推荐答案
我想你想使用 eval()
函数.
I think you want to use the eval()
function.
在评估之前对数据库中的数据进行清理时要非常小心,因为您可能允许用户执行您不希望他们执行的 PHP 代码.
Be very careful about sanitising the data from the database before evaling it though as you could allow users to execute PHP code that you don't want them to.
$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))
{
这篇关于如何制作php表达式并从数据库执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!