简体   繁体   中英

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

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;

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.

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;
    }


?>

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM