PHP issues in a loop

I'm primarily a guy from CSS and HTML, but I recently ventured into PHP.

I do not understand why this script hangs:

$loop_Until = 10;

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

Can anybody help?

+5
source share
4 answers

This calls the ifinate loop, and you'll want to take a look at the php loop for. http://php.net/manual/en/control-structures.for.php

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

You increase $loop_Untilevery time and never increase $i, therefore $ithere will always be less$loop_Until

+11
source

Fixed code

$loop_Until = 10;
$i = 0;

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

Code Explanation:

// 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;
}
+18
source

: "+" "-" . . :

$loop_Until = 10;

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

, .

, $i , $loop_Until, 1 $loop_Until $loop_Until = $loop_Until + 1; , $i , $loop_Until.

$loop_Until, $i.

1 , --$variable. 1 , , ++$variable, :

$loop_Until = 10;

while($i < $loop_Until)
{
    // do some code here
    --$loop_Until;
}

, $loop_Until -, , , , . $i . , $i , (, $loop_Until, , while ), :

$loop_Until = 10;

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

, ++ $i , $i ++

, for . :

for($loop_Until = 10; $i < $loop_Until; --$loop_Until)
{
    // do some code here
}

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

, , .

Finally, which of these solutions you choose will depend on whether you want $ i or $ loop_Until to remain unchanged.

If you have multiple loops and want to do all of them the same number of times, it would probably be nice to leave $ loop_Until untouched and reset $ i at the beginning of each loop.

+1
source

while( 0 != ($loop_until--) );

0
source

All Articles