Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP while loop problems

Tags:

loops

php

I am primarily a CSS and HTML guy but I have recently ventured into PHP.

I can't see why this script hangs:

$loop_Until = 10;

while($i < $loop_Until)
{
    // do some code here
    $loop_Until = $loop_Until + 1;
}

Can anyone please help?

like image 428
greenie Avatar asked Jul 08 '10 16:07

greenie


People also ask

How can I print 1 to 10 numbers in PHP?

We can print numbers from 1 to 10 by using for loop. You can easily extend this program to print any numbers from starting from any value and ending on any value. The echo command will print the value of $i to the screen. In the next line we have used the same echo command to print one html line break.

How is PHP looping done?

In PHP, we have the following loop types: while - loops through a block of code as long as the specified condition is true. do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true. for - loops through a block of code a specified number of times.

How do you print even numbers in PHP while loop?

php $end=50; $even= "Even Numbers Are : "; $odd="<br /> Odd Numbers Are : "; for($i=1;$i<=$end;$i++) { if($i%2==0) { $even. =$i.","; }else $odd.


1 Answers

Fixed Code

$loop_Until = 10;
$i = 0;

    while($i < $loop_Until)
    {
        // do some code here
        $i = $i + 1;
    }

Explanation of your code:

// A variable called loop until is set to 10
$loop_Until = 10;  

// While the variable i is less than 10
// NOTE:  i is not set in code snippet, so we have no way of knowing what value it is, if it is greater than 10 it might be infinite
while($i < $loop_Until)
{
    // Increment the 10 value up 1 every time, i never changes!
    $loop_Until = $loop_Until + 1;
}
like image 110
Tom Gullen Avatar answered Sep 29 '22 19:09

Tom Gullen