0

I want to make simple program that change operators "+", "-", "*", "/" for some numbers. So, I put operators in array, and try to iterate them through loop.

$num1 = 10;
$num2 = 20;
$operators = array("+", "-", "*", "/");
for ($x=0;$x<=count($operators)-1;$x++){
echo $num1 . $operators[$x] .  $num2 . "</br>";
}

It displays:

10+5
10-5
10*5
10/5

That seems ok, at first glance, but I need numbers to be calculated, operations performed, simply, I need final result numbers, and this gives me 4 strings. I understand reason for this: my values in $operators array are strings, not real operators. My question is, how to put real operators in array, or maybe, I can keep them as strings in array, but somehow convert them in real operators at the output? Solutions for both strategies are welcome. Thanks in advance!

wanderer
  • 19
  • 3
  • possible duplicate of [calculate math expression from a string using eval](http://stackoverflow.com/questions/18880772/calculate-math-expression-from-a-string-using-eval) – Rizier123 Feb 22 '15 at 14:39
  • Thanks for your help, man. I visited the page, but that would not work for me, for the reason I gave in answer to RamRaider. Thanks, again. – wanderer Feb 22 '15 at 15:33

3 Answers3

0

perhaps you could try somehting along the following lines:

for ( $x=0; $x < count($operators); $x++ ){
    switch($operators[$x]){
        case '+':$answer=$num1+$num2;break;
        case'-':$answer=$num1-$num2;break;
        case '*':$answer=$num1*$num2;break;
        case'/':$answer=$num1/$num2;break;
    }
    echo $answer;
}
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • I simplified my script for this question purpose, but in reality my goal was to iterate all combinations of 4 numbers (that does not repeat ) with math operators. So, your answer is good, if the script is small (each "case '': " in "switch" statement for one combination), but I have 1972 combinations. So that strategy would be pointless. Is that right? Thanks man, your answer is good, but I need some other solution. – wanderer Feb 22 '15 at 15:31
  • @wanderer then you should edit your question with the real expectations – rjdown Feb 22 '15 at 15:37
  • I'll do it, I just wanted to spare members from unnecessary information, but it turns out information is not irellevant. – wanderer Feb 22 '15 at 15:48
0

You can't put real operators in array as it's language contructions. But you can put functions like that (I use anonymous functions, you can use named)

$operations = array(
    '+' => function ($a, $b) { return $a + $b; }
);

foreach ($operations as $sign => $func) {
   echo '10'.$sign.'5 = '. $func(10, 5)."\n";
}
SeriousDron
  • 1,296
  • 9
  • 12
0

You may try this

$num1 = 10;
$num2 = 20;

$operators = array("+", "-", "*", "/");
for ($x=0;$x<=count($operators)-1;$x++){
    echo eval('return '.$num1 . $operators[$x] .  $num2 . ';')."</br>";
}
Shraddha
  • 1
  • 2
  • yes it does work. eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. – Shraddha Feb 22 '15 at 16:27
  • Thanks, it actually works. I suppose eval() can't be bad unless it is available for users (external input), which is not the case here. Right? Thanks again. – wanderer Feb 23 '15 at 14:09