tags:

views:

138

answers:

5

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;
}
+2  A: 

You can use eval(), but it's generally a bad idea, since it's a major potential security hole (be careful you're not allowing visitors to run arbitrary code!).

It can also result in hard to maintain code.

therefromhere
+1  A: 

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;
}
Hannes de Jager
+2  A: 

Quick answer is eval(). However, in this precise example I would just hard-code the possible operations:

<?php

$operator = '+';
foreach($resultSet as $item){
    switch($operator){
        case '+':
            $result = $item[$this->orderField] + 1;
            break;
    }
    echo $result;
}

?>
Álvaro G. Vicario
+4  A: 

To achieve exactly that, you can use create_function

$operator = '+';
$func = create_function('$a,$b', "return \$a $operator \$b;");
foreach($resultSet as $item){
    $result = $func($item, 1);
    echo $result;
}

A cleaner solution is possible with lambdas (php5.3 required)

$func = function($a, $b) { return $a + $b; };
foreach($resultSet as $item){
    $result = $func($item, 1);
    echo $result;
}

See also array_sum, array_reduce

Advanced example with array_reduce and lambdas

$ary = array(
    array('foo' => 1, 'bar' => 91),
    array('foo' => 2, 'bar' => 92),
    array('foo' => 3, 'bar' => 93),
    array('foo' => 4, 'bar' => 94),
    array('foo' => 5, 'bar' => 95),
);

$sumOfFoos = array_reduce($ary, 
    function($val, $item) { return $val + $item['foo']; } 
);
$sumOfBars = array_reduce($ary, 
    function($val, $item) { return $val + $item['bar']; } 
);

The main point is, that instead of 'variable operators' (which is not possible in php), you should rather use variable functions (which is possible and much more flexible).

stereofrog
+1 but `create_function()` will still `eval()` your code
Andy
it will eval my _code_ but not _variables_, which is the key difference.
stereofrog
But either $operator is an arbitrary variable, too, or you don't need create\_function (like in reko\_t's answer), do you?
VolkerK
+10  A: 

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;
}
reko_t
+1 for avoiding `eval`
Matt
+1 - Cheers reko_t.
BenTheDesigner
Use an arbitrary precision math library to perform regular math? Looks kind of quirky...
Álvaro G. Vicario
Develop with extensibility in mind Alvaro... Who says the same function may not need to perform precision mathematics?
BenTheDesigner