Variable Operators in PHP

前端 未结 5 851
青春惊慌失措
青春惊慌失措 2020-12-07 02:15

Given this example, how would I return the result of the equation rather than the equation itself as a string?

$operator = \'+\';
foreach($resultSet as $item         


        
5条回答
  •  猫巷女王i
    2020-12-07 03:11

    Use the eval function of PHP: http://php.net/manual/en/function.eval.php

    $operator = '+';
    foreach($resultSet as $item){
        $result = $item[$this->orderField] . $operator . 1;
        eval("\$result = \"$result\";");
        echo $result;
    }
    

提交回复
热议问题