eval函数错误,如何解决?

时间:2018-11-15 12:31:36

标签: php

这是php代码,我该怎么办? 它显示一个错误错误:解析错误:语法错误,C:\ xaam \ htdocs \ abacus \ stack.php(36)中意外的')',第1行上的eval()代码

 <?php

   $potentialOperands = array('+', '-', '*', '/');  //Keep division as 
    the last operand here
   $previousInt = null;        //Track the previous operand, else we'll 
   get division by zero

   $randomIntegers = array();
    $randomOperands = array();

    // do a loop over $i n times
     for ($i = 0; $i <=4; $i ++) {
  // assign random operand to array slot
   $nextInt = rand(0, 9);
   $randomIntegers[] = $nextInt;
     if($i < 10){    //No operand after last integer
    if($previousInt === 0){
        //Make sure to avoid a potenial division-by-zero
        $randomOperands[] = $potentialOperands[rand(0, 2)];
    }
    else{
        $randomOperands[] = $potentialOperands[rand(0, 3)];
    }
   }
    $previousInt = $nextInt;
  }

 // print array values:
  $exp = '';
   foreach($randomIntegers as $key=>$value1)
   {
   $exp .=  $value1 . " "; 
   if(isset($randomOperands[$key])){
       $exp .=  $randomOperands[$key] ." "; 
   }
  }

  $res = eval("return ($exp);");

 //Generates answer rounded to 3 decimal places
  //The answer uses standard PHP operator precedence
   printf("%s = %.3f", $exp, $res);
  ?>

0 个答案:

没有答案