Can You 'Exit' a Loop in PHP

Can you 'exit' a loop in PHP?

You are looking for the break statement.

$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list(, $val) = each($arr)) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}

Break, continue and exit issue while using with loop PHP 5.6

break ends execution of the current for, foreach, while, do-while or switch structure.

break 1 will only break out of your "switch" statement, you will need break 2 to reach the while loop nesting level.

$count = 0;
while( $count++ < 10 )
{
echo 'while';
switch( true )
{
case true:
echo 'switch';
break 2; // TWO is needed here to break out of TWO levels of nesting
}
}

https://www.php.net/manual/en/control-structures.break.php

break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. The default value is 1, only the immediate enclosing structure is broken out of.

PHP - Exit For Loop After Wrong Answer

You are missing your brackets in your for loop. I'm surprised the elseif is the culprit and that the code doesn't fail anyways. But here is what I would do, errors aside:

$correct = false;

for ($i = 0 ; $i < count($stateCapitalNames); $i++){
if ($enteredCity == $stateCapitalNames[$i]) {
$correct = true;
$stateNames = $stateNames[$i]; // Updated $stateNames variable

break;
}
}

//You can check $correct here...
if($correct){
print "<p>$enteredCity is the capital of <b>$stateNames[$i]</b>. </p>"; /*Removed [$i] from $stateNames. For some reason, $stateNames[$i] wasn't updating outside the loop, but now it is.
}

This way, no matter what, until the code finds a correct answer, the user is wronge. Once it finds the right answer, it sets it as correct and exits the loop by setting $i to the length of the array.

PHP - Break after return?

It will run just once, stop looping, and exit from the function/method.

It could be argued though that this is bad style. It is very easy to overlook that return later, which is bad for debugging and maintenance.

Using break might be cleaner:

for($i = 0; $i < 5; $i ++) {
if($var[$i] === '')
{ set_some_condition;
break;
}
}

if (some_condition)
return;

Exit from a loop in php in a wordpress envirnoment

You should try indenting your code to see clearer what is happening.

Why do you this:

$counter++;
if ($counter % 1 == 0) {
echo '<li>';
}
if ($counter == false) {
echo '</li>';
}

Your $counter is not initialized, your $counter % 1 will always result $counter.
Couldn't understand what are you trying to achieve here.

Anyway, why don't you put the "

  • " items like this:

    echo '<li><h4>' . get_post_meta($post->ID, 'name', true) . '</h4></li>' ;

    Then you won't need those 2 strange IF's anymore. Not even to break the while loop, since it will stop when were no more posts.

    break out of if and foreach

    if is not a loop structure, so you cannot "break out of it".

    You can, however, break out of the foreach by simply calling break. In your example it has the desired effect:

    $device = "wanted";
    foreach($equipxml as $equip) {
    $current_device = $equip->xpath("name");
    if ( $current_device[0] == $device ) {
    // found a match in the file
    $nodeid = $equip->id;

    // will leave the foreach loop and also the if statement
    break;
    some_function(); // never reached!
    }
    another_function(); // not executed after match/break
    }

    Just for completeness for others that stumble upon this question looking for an answer..

    break takes an optional argument, which defines how many loop structures it should break. Example:

    foreach (['1','2','3'] as $a) {
    echo "$a ";
    foreach (['3','2','1'] as $b) {
    echo "$b ";
    if ($a == $b) {
    break 2; // this will break both foreach loops
    }
    }
    echo ". "; // never reached!
    }
    echo "!";

    Resulting output:

    1 3 2 1 !

    Break while loop inside for loop

    It is

    break 2;

    References:

    • http://php.net/break

    ...

    PHP - exit from IF block

    Why not just turn it around.

    if($bla): 
    $bla = get_bla();
    if(!empty($bla)) {
    do($bla);
    }
    endif;

    That way it will only run your code if $bla isn't empty.. That's kinda the point with if-statements

    How do I break a for-loop in PHP when conditions are met?

    break; #breaks out of a loop
    continue; #skips rest of current iteration
    • http://us3.php.net/manual/en/control-structures.break.php
    • http://us3.php.net/manual/en/control-structures.continue.php


    Related Topics



  • Leave a reply



    Submit