Home > Backend Development > PHP Tutorial > 初学者以前从未碰过编程的,写了一个函数关于简单计算的一些疑问

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

WBOY
Release: 2016-06-23 14:16:24
Original
861 people have browsed it

  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.请大家指导指出错误,万分感谢


回复讨论(解决方案)

所有判断的地方
$act ='+'
应该是 ==而不是赋值。
$act == '+'
$act == '-'
$act == '*'
$act == '/'

马上改过来ok!谢谢了!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template