Home  >  Article  >  Backend Development  >  PHP函数求一个整数的阶乘

PHP函数求一个整数的阶乘

不言
不言Original
2018-05-23 16:20:113674browse

//随手写的一个阶乘的函数

注意:不要把负数给忘了!不要把负数给忘了!不要把负数给忘了!

<?php 
function jisuan($i){
if($i==0){                                   
return 1;
}elseif($i<0){    

$rest=$i*jisuan($i+1);                
//负数往0靠近是+1     
return $rest;
}else{    
$rest=$i*jisuan($i-1);                  
//正数往0靠近是-1    
return $rest;    
}  
}  
echo jisuan(-7);
?>
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