Home > Article > Daily Programming > How to find the factorial of 6 using PHP for loop
If you want to use PHP for loop to find the factorial of a certain number, We first need to know what is the factorial of a number? The factorial of a number is the product of all integers, including this number. For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24, and the factorial of 5 is 5 * 4 * 3 * 2 * 1 = 120.
Below we will use the PHP for loop code example to calculate the factorial of 6 for everyone.
The code example is as follows:
<?php $n = 6; $x = 1; for($i=1;$i<=$n-1;$i++) { $x*=($i+1); } echo "$n 的阶乘 = $x"."\n";
The calculation result is as follows:
6 的阶乘 = 720
Note: In the above code, the expression $x *= ($i 1) Equivalent to $x = $x * ($i 1);
This article is an introduction to the method of using PHP for loop to calculate the factorial of 6. It is also one of the common PHP interview questions. 1. It is very simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to find the factorial of 6 using PHP for loop. For more information, please follow other related articles on the PHP Chinese website!