Home  >  Article  >  Backend Development  >  菜鸟以前从未碰过编程的,写了一个函数关于简单计算的一些疑问

菜鸟以前从未碰过编程的,写了一个函数关于简单计算的一些疑问

WBOY
WBOYOriginal
2016-06-13 12:41:59853browse

初学者以前从未碰过编程的,写了一个函数关于简单计算的一些疑问
  function sum($num1,$act,$num2){
  
  if ($act!='+'&&$act!='-'&&$act!='*'&&$act!='/') {
        echo $res=null;
        exit;
  }elseif ($act='+') {

     $res=$num1+$num2;
   
     
 
  }
  elseif ($act='-') {
         $res= $num1-$num2;
         
  }
  elseif ($act='*') {
       $res=$num1*$num2;
    
  }
  elseif ($act='/') {
  if ($num2==0) {
        echo '0不能做被除数,
';
   } else {
          $res= $num1/$num2;
         
   }        
  }

return $res;
}
echo sum (10,'+',2);
  ?>
为什么只能算加法,而$act赋‘-’‘*’‘/’算出来的结果都是12.请大家指导指出错误,万分感谢

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn