Home  >  Article  >  Backend Development  >  PHP implements factorial operation small code

PHP implements factorial operation small code

WBOY
WBOYOriginal
2016-07-23 08:54:561294browse
  1. $sum = 1;
  2. $num = 7;
  3. if( $num>0 ){
  4. for($i=$num;$i>0;$i--){
  5. $sum*=$i;
  6. }
  7. echo $sum;
  8. }elseif( $num<0 ){
  9. //Note here is ++, negative numbers closer to 0 are ++
  10. for($i=$num; $i<0;$i++){
  11. $sum*=$i;
  12. }
  13. echo $sum;
  14. }else{
  15. echo "0";
  16. }
  17. ?>
Copy code

PHP


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