2

Please check my code:

<?php
  $operarors = array( '+', '-', '*' );
  $randOperator = array($operarors[rand(0,2)], $operarors[rand(0,2)]);
  $num1 = rand(0,10);
  $num2 = rand(0,10);
  $num3 = rand(0,10);

  $result = $num1.$randOperator[0].$num2.$randOperator[1].$num3;
  echo "The math: $num1 $randOperator[0] $num2 $randOperator[1] $num3 = $result";
?>

In the code above, I am not getting my total number of result.

Suppose I am getting 3+4*5, the output should be 23, but it is showing the string 3+4*5.

Help me please.

3 Answers 3

5

You can't just concatenate the operators like that. I suggest doing something like this:

<?php
function operate($op1, $operator, $op2) {
    switch ($operator) {
        case "+":
            return $op1 + $op2;
        case "-":
            return $op1 - $op2;
        case "*":
            return $op1 * $op2;
    }
}

$operators = array( '+', '-', '*' );

// performs calculations with correct order of operations
function calculate($str) {
    global $operators;

    // we go through each one in order of precedence
    foreach ($operators as $operator) {
        $operands = explode($operator, $str, 2);
        // if there's only one element in the array, then there wasn't that operator in the string
        if (count($operands) > 1) {
            return operate(calculate($operands[0]), $operator, calculate($operands[1]));
        }
    }

    // there weren't any operators in the string, assume it's a number and return it so it can be operated on
    return $str;
}

$randOperator = array($operators[rand(0,2)], $operators[rand(0,2)]);
$num1 = rand(0,10);
$num2 = rand(0,10);
$num3 = rand(0,10);

$str = "$num1 $randOperator[0] $num2 $randOperator[1] $num3";

echo "$str = ", calculate($str), PHP_EOL;
Sign up to request clarification or add additional context in comments.

6 Comments

but with 2+1*3 result will 2+3 = 3 * 3 => 9 ? but 5 is expected
@MouradK you re right. i am getting error output The math: 3 - 5 * 5 = -22 but output is = -10. This is not a right answer.
@AndreaFalds your code is not working this case 3 - 6 * 5 = -15 (Wrong output). Expected Answer -27
@MouradK I've updated the code to handle order of operations/operator precedence. The code wasn't wrong before, I just didn't know you wanted it to handle that.
@Chinu The code does work, it just wasn't designed to handle order of operations. I've changed it to do so.
|
1

As @AndreaFaulds said, or use callbacks: (though using array_reduce and all this array pointer magic is not necessary).

<?php

$ops = [
    '+' => function ($op1, $op2) { return $op1 + $op2; },
    '*' => function ($op1, $op2) { return $op1 * $op2; },
    '-' => function ($op1, $op2) { return $op1 - $op2; }
];

$nums = [rand(0, 10), rand(0, 10), rand(0, 10)];
$operators = [array_rand($ops), array_rand($ops)];

$initial = array_shift($nums);
$result = array_reduce($nums, function ($accumulate, $num) use (&$operators, $ops) {
    return $ops[each($operators)[1]]($accumulate, $num);
}, $initial);

Note, [] short array syntax has a version requirement of PHP 5.4+.

3 Comments

Parse error: syntax error, unexpected '[' in E:\xampp\htdocs\test\test.php on line 2
@chatfun It's syntax sugar added in PHP 5.4: php.net/manual/en/language.types.array.php
@KevinHerrera this code is not working. can you please change your answer.
-3

You should use the + operator instead of the . operator. The . just pastes it together as if the values were strings.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.