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

PHP函数求一个整数的阶乘

不言
Release: 2023-02-28 16:00:01
Original
3711 people have browsed it

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

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

<?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);
?>
Copy after login
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