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

WBOY
Release: 2016-06-13 12:41:59
Original
860 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.请大家指导指出错误,万分感谢

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!