Understanding "Formula" Code

Hello,

Came across this piece of code for a Factorial “formula” and can’t seem to get it:

[php]

Factorial Test


<?php
$var = rand % 10;

        $counter = $var;
        
        $echo_var = $var;
        
        while ($counter > 1) {
            $var = $var * ($counter - 1);
            
            $counter--;
        }
        
        echo "The Factorial of $echo_var is: $var";
    ?>
</body>
[/php]

Could an expert kindly explain how the code works (step by step, if possible).

Thanks in advance.
Chris

the code was not working for me so i changed
[php]
$var = rand (0,10);
[/php]
[php]

Factorial Test <?php //call a php built in function and assign the value to variable $var. $var = rand (0,10);
	 //use the $var and assign to the $counter variable
        $counter = $var;
    //use the $var and assign to the $echo_var variable  
        $echo_var = $var;
     
	 
	//iterate throught a loop (repeat the same code block until a condition is met)
	// while the value of $counter is greater than 1 
        while ($counter > 1) 
		{
		//multiply $var times $counter -1
            $var = $var * ($counter - 1);
			
			//update counter
            $counter--;
        }
        //print the result.
        echo "The Factorial of $echo_var is: $var";
    ?>
</body>
[/php]

Thanks, Wilson382.

I was wondering about that modulus.

Your explanation makes perfect sense.

Chris

Sponsor our Newsletter | Privacy Policy | Terms of Service