簡體   English   中英

如何使用從數據庫中提取的條件運算符並使用 PHP 添加到腳本中

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

我將條件運算符存儲在數據庫中,因為我們有一堆不同的條件,並且需要動態地將其插入到代碼中。 因此,我從數據庫中提取條件運算符,然后將其存儲在變量中。 但是我需要在我的 if 語句中實際使用該運算符。 如何將其插入 if 語句並將其用作常規條件運算符?

下面是一些代碼示例:

$cond_operator = "<"; // (but this opperator actually pulled from database)

if ( $actual_value $cond_operator $score_value ) {
    return $result;
}

你可以做你展示的唯一方法是使用eval() 但通常不贊成使用eval()因為它引入了許多潛在的安全漏洞。

我會這樣做硬編碼,以避免eval()並確保我可以控制我想要支持的特定運算符:

$cond_operator = "<"; // (but this operator actually pulled from database)

switch ($cond_operator) {
  case "<":
    if ( $actual_value < $score_value ) {
      return $result;
    }
    break;
  case ">":
    if ( $actual_value > $score_value ) {
      return $result;
    }
    break;
  case "==":
    if ( $actual_value == $score_value ) {
      return $result;
    }
    break;
  default:
    trigger_error("Unsupported condition operator: '$cond_operator'");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM