代码之家  ›  专栏  ›  技术社区  ›  Marcos Felipe

基于存储的变量进行比较

  •  0
  • Marcos Felipe  · 技术社区  · 8 年前

    也许这有个话题,但我找不到。

    我在数据库中存储一个比较运算符和一个目标值,如:

    $target_value_1 => 1000
    
    $operator => greather_than_or_equal
    

    有没有办法在变量中使用这些运算符,例如:

    if ($variable1 $operator $target_value_1){
    
        // Some code
    
    }
    

    使用PHP和MySQL。

    谢谢

    1 回复  |  直到 8 年前
        1
  •  1
  •   mrbm    8 年前

    我不确定您正在构建什么,但您可以通过创建 check($operator, $var1, $var2) 功能大致如下:

    function check($operator, $val1, $val2) {
        switch ($operator) {
        case 'greather_than_or_equal':
            return $val1 >= $val2;
            break;
        case 'greather_than':
            return $val1 > $val2;
            break;
        case 'operator_name':
            //return your condition goes here;
            break;
        default:
            return false;
            break;
        }
    }
    

    并使用它: if (check($operator, $value1, $value2)) { /* your code */ }

    推荐文章