PHP中的变量运算符 [英] Variable Operators in PHP

查看:184
本文介绍了PHP中的变量运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出此示例,我将如何返回方程式的结果而不是将方程式本身作为字符串返回?

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){
    $result = $item[$this->orderField] . $operator . 1;
    echo $result;
}

推荐答案

您可以制作包装操作符的函数,或者为简单起见,只需使用

You could make functions that wrap the operators, or for simplicity just use the bc extension:

$operator = '+';
$operators = array(
  '+' => 'bcadd',
  '-' => 'bcsub',
  '*' => 'bcmul',
  '/' => 'bcdiv'
);

foreach($resultSet as $item){
    $result = call_user_func($operators[$operator], $item[$this->orderField], 1);
    echo $result;
}

这篇关于PHP中的变量运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆