PDA

View Full Version : Script questions


Doug.Mellon
09-06-2005, 05:47 PM
Hey,
As of right now I am in the process of learning PHP and I have a question. In the W3 tutorial there is a script that looks like this:


<html>
<body>

<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>

</body>
</html>


And I was wondering how $1++; makes the number stop at 5 as when I delete that it just keeps on going and doesnt stop.
Thanks in advance.
Adios,
Doug Mellon
www.a5monarchy.com

hammerstein_04
09-06-2005, 06:13 PM
The while statement assess the value of $i on each pass. If it is less than or equal to 5 then it repeats the statements within the while loop over again.

The last statement in this while loop increments $i, when it reaches 6 the condition within the while ($i <= 5 ) will no longer be true.

If you remove the <=5 or you remove everything in the brackets then the loop will continue infinitely until something get's it out. Infinite loops are not something you want. You need to make sure there is a control statement within the loop that will break ( I think break works) and continue processing after the while statement.

Doug.Mellon
09-06-2005, 06:29 PM
Cool thanks man