I am trying to run the while loop until it equals to thirty.
<?php
$num1=0;
$num2=0;
$sum= $num1 + $num2;
while($sum=30){
$num1++;
$num2++;
echo "$sum is equal to 30";
}
?>
You need to change while($sum=30) to while($sum<30).
Then, the while loop will end after you reached 30. The echo then comes after the closing bracket.
Sp your working code will look like this:
while( $sum < 30 )
{
$num1++;
$num2++;
$sum = $num1 + $num2;
}
echo "sum is $sum (which is 30)";
echo "$sum is 30"; will print 32 is 30You are calculating the sum outside the while loop, so inside the loop the $sum never changes. You have to calculate the sum inside the loop. also = is assignment operator. you must use comparison operator to compare.
$num1=0;
$num2=0;
$sum=0; //initilize you sum to 0
while($sum<30){ // loop while your sum is less than 30
$sum= $num1 + $num2; //calculate the sum
$num1++;
$num2++;
}
echo "$sum is equal to 30";
?>
Reference:Comparison Operators
!= in your case, but if you start with $num1=1; or $num2=1; you may never hit sum of 30. so in that case < helps.
$sum= $num1 + $num2;should be inside thewhileloop or it is going to be a infinite loop.