-1

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

Salman A
  • 262,204
  • 82
  • 430
  • 521
mydeve
  • 553
  • 1
  • 14
  • 39
  • What you are missing is, that you only concatenate the values which results in a string. You are not performing any operations on/with the values. –  Nov 30 '14 at 08:52
  • You could receive division by zero error. Keep this in mind – sectus Nov 30 '14 at 09:13

4 Answers4

1

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;
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • And what is if it's like: `5/0` ? – Rizier123 Nov 30 '14 at 09:01
  • Divide overflow obviously. – Salman A Nov 30 '14 at 09:02
  • @Rizier123 , Is it possible that OP creates a test to retrieve division by zero error with random appear? – sectus Nov 30 '14 at 09:03
  • @sectus I don't see one in OP's code! And in this solution neither. – Rizier123 Nov 30 '14 at 09:04
  • 1
    @Rizier123 , I mean that you do not know the real problem of OP. So additional checking for this error could be an overenginiting. : ) – sectus Nov 30 '14 at 09:06
  • @sectus But then i have to tell you that i expect from someone with such much reputation to think about this and point out to OP, that he has to check that! – Rizier123 Nov 30 '14 at 09:09
  • @Rizier123 , you are right. And do not look at rep, and your reputation groth speed is higher than mine. :^ ) – sectus Nov 30 '14 at 09:12
  • @Rizier123 higher reputation is not proportional to longer answers. If I must add additional points then I would rather begin my answer with: _what if I told you that `rand()` is not random enough_. – Salman A Nov 30 '14 at 09:17
1

if I'm reading your question right, you're computing a string, not applying the operator. You must apply code to rand1 and rand2 to compute the final value.

The easy way is to eval $finaalvalue ($finalvaalue = eval("return $finaalvalue ;");) before echoing it.

If you need the code to run fast, or don't trust the input, use a switch or a function map:

$operatorMap = array(
    '+' => function($a, $b) { return $a + $b; },
    '*' => function($a, $b) { return $a * $b; },
    ...
);
$finaalvalue = $operatorMap[$operator]($rand1, $rand2);

PHP anonymous functions run slow compared to methods and normal functions, so avoid them in tight loops where speed matters.

Is this a homework question? You should do your own work.

Andras
  • 2,995
  • 11
  • 17
0

This should work for you:

(Also if you do random calculation's you would have to check that there is no division by zero)

<?php

    function calculate_string( $mathString )    {
        $mathString = trim($mathString); 
        $mathString = str_replace ('[^0-9\+-\*\/\(\) ]', '', $mathString);    

        $compute = create_function("", "return (" . $mathString . ");" );
        return 0 + $compute();
    }

    $rand1 = rand(0,9);
    $rand2 = rand(0,9);
    $operator = array('*','/','+','-');
    $randoperator = $operator[rand(0,3)];

    if($operator = "/" && $rand2 == 0)
        echo "Division by zero!";
    else {
        $finaalvalue = calculate_string($rand1 . $randoperator . $rand2);
        echo $rand1.$randoperator.$rand2.'='.$finaalvalue;
    }


?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

Suggest you to calculate mathematics operation by switch case. Your system can't perform mathematics operation, it will generate a string. That is concatenation.

$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 '/':
        if(!$rand2) $rand2++; 
        $finaalvalue = $rand1 / $rand2;
        break;
    case '*':
        $finaalvalue = $rand1 * $rand2;
        break;
}
echo $rand1.$randoperator.$rand2.'='.$finaalvalue;
MH2K9
  • 11,951
  • 7
  • 32
  • 49