Q:

php random operator and their operations

see my code below
$rand1 = rand(0,9);
$rand2 = rand(0,9);
$operator = array('*','/','+','-');
$randoperator = $operator[rand(0,3)];
$finaalvalue = $rand1."$randoperator".$rand2;

echo $rand1.$randoperator.$rand2.'='.$finaalvalue;

i want to take two random number and do random operation like +,-,*,/ and get their value
like 2-5=6
there is some problem while doing is what am i missing

A:

The obvious answer is to use the eval function, but its use is highly discouraged:

Caution   The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is
  discouraged. If you have carefully verified that there is no other
  option than to use this construct, pay special attention not to pass
  any user provided data into it without properly validating it
  beforehand.

The other option is to write separate blocks of code that perform the desired operation, then choose the appropriate branch, for example, by using a switch...case statement:
<?php
$rand1 = rand(0, 9);
$rand2 = rand(0, 9);
$operator = array('*', '/', '+', '-');
$randoperator = $operator[rand(0, 3)];
switch ($randoperator) {
    case "+":
        $finaalvalue = $rand1 + $rand2;
        break;
    case "-":
        $finaalvalue = $rand1 - $rand2;
        break;
    case "*":
        $finaalvalue = $rand1 * $rand2;
        break;
    case "/":
        $finaalvalue = $rand1 / $rand2;
        break;
}
echo $rand1 . $randoperator . $rand2 . '=' . $finaalvalue;

