program that calculates the number factorial

1

I'm trying to make a program that calculates the factorial of a number.

I did it with a while and if it worked for me but I try it with a do while but it does not give me any results

this is with while

<?php
$num=5;
$actorial=1;
$w=$num - 1;

while($w>=1){
    echo $actorial ."x". $w. "=";

    $actorial=$actorial * $w;
    echo $w--;

}
echo $actorial;

?>

throws this:

  

1x4 = 44x3 = 312x2 = 224x1 = 124

with do while

<?php
$w=$num-1;
$num=5;
$actorial=1;
do{

    echo $actorial ."x". $w. "=";
   $actorial=$actorial * $w;
   echo $w--;
   echo $actorial;


}while($w>=1);

?>
    
asked by wdwd 04.10.2017 в 09:52
source

2 answers

0

The variable did not exist when you tried to access it. On the other hand I have removed $w-- so that it does not appear on the screen, since I was doing something messy.

Here is corrected and working:

$num=5;
$w=$num-1;
$actorial=1;
do{

   echo $actorial ."x". $w. "=";
   $actorial=$actorial * $w;
   $w--;
   echo $actorial;
   echo "  ";


}while($w>=1);
    
answered by 04.10.2017 / 10:24
source
0

The problem is the order of declaration of the variables the code should be:

<?php
$num=5;
$w=$num-1;
$actorial=1;
do{
...
    
answered by 04.10.2017 в 10:18