Skip to content
Advertisement

How to out of while and for loop combined

I am trying to write a code that is capturing number 3. using while loop as the condition, so that the for statement will be the evaluation condition of $x, of the while loop statement, and by the same time, using if statement to evaluate the value of $x=3 and so it can echo ‘three..’; . please enlighten me. thank you

<?php
$x = 0;
$y = 5;

while ($x <= $y) {
    for ($z = 0; $z < 3; $z++) {
        if ($x = 3) {
            echo 'three..' . "n";
        }
    }
    $y++;
}

Advertisement

Answer

In while loop, you should increment x variable. If you increment y variable, it will always “true”. So it will be endless loop.

<?php
 $x = 0;
 $y = 5;

while ($x <= $y) {
   // code
    $x++;
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement