This forum is now read-only. Please use our new forums at discuss.codecademy.com.

While Loops in PHP Forum

View Course » View Exercise

481 points
F7b57921d645a3a4c6cf0dd422c6a224?s=140&d=retro
Submitted by
aaw588
over 4 years ago

Do/while loop "taking too long"

Hi guys.
I'm going crazy here with this. I can't figure out what's going wrong in my do/while loop. The goal is to count down from 50 by 5 but the editor never outputs what I expect it to, it just says "Program is taking too long." Can somebody help me understand what I'm doing wrong?
enter code here<!DOCTYPE html>
<html>
<head>
<title>Your own do-while</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
$number = 50;
do {
echo "{$number} ";
if ($number > 0) {
$number - 5;
$truth = true;
}
else {
$truth = false;
}
}
while ($truth);
echo "We're done!";
?>
</body>
</html>


1 vote

permalink

The issue might be with this line of code:

$number - 5;

That doesn't change the value stored in your $number variable. I think you need to set $number to $number - 5 instead:

$number = ($number - 5);

Otherwise, the value of $number will forever be 50, and you'll get an infinite loop.

756 points
51774f974ae5f55804001fe1_784606691
Submitted by
Derek Spencer
over 4 years ago

2 Comments

F7b57921d645a3a4c6cf0dd422c6a224?s=140&d=retro aaw588 over 4 years ago

Yup! That was the line! Another fix was changing the line to "$number -= 5; "

Thanks!

51774f974ae5f55804001fe1_784606691 Derek Spencer over 4 years ago

Great! Glad everything worked out.