简体   繁体   中英

How to break out of a while loop from a nested include file within PHP 7?

I have a while loop that includes several complex functions as it loops through Mysql records. One of the more simple tasks is to check the variable $city for a ñ symbol, and if $city contains a ñ symbol, then the MySql record needs to be skipped, but the loop needs to continue for the rest of the Mysql records.

In the past, I have used "break;" for this, but I am receiving this error:

PHP Fatal error: 'break' not in the 'loop' or 'switch' context

I have read that I should use "return false;" instead, but this does not seem to be working either.

Any help is much appreciated.

test.php

<?PHP

$query = "SELECT * FROM Cities limit 5";
$result = mysqli_query($con, $query);

while($row = mysqli_fetch_assoc($result))
    {
    $city = $row['City'];
    $city = "ñ";  // I hardcoded for testing
    require "test_function.php";
    echo "I should not see this if there is a ñ within $city \n\n";
}

?>

test_function.php

<?PHP

if( strpos( $city, "ñ" ) !== false) {
    echo "City $city contains a ñ.  Skipping...\n\n";
        break;
}

?>

If you just want to skip one city, you should use continue , not break . continue goes to the next loop iteration, break ends the loop completely.

However, these statements have to be inside the loop body, they can't be in a function that it calls or an include file.

What you can do is have the include file set a variable, and check that after it.

test_function.php

<?PHP

if( strpos( $city, "ñ" ) !== false) {
    echo "City $city contains a ñ.  Skipping...\n\n";
    $skip_city = true;
} else {
    $skip_city = false;
}

?>

test.php

Why are you including other php file in the loop? Apply the condition within the loop and it would break out of the loop if condition is matched.

<?PHP

$query = "SELECT * FROM Cities limit 5";
$result = mysqli_query($con, $query);

while($row = mysqli_fetch_assoc($result))
    {
    $city = $row['City'];
    if( strpos( $city, "ñ" ) !== false) {
        echo "City $city contains a ñ.  Skipping...\n\n";
        break;
    }
    echo "I should not see this if there is a ñ within $city \n\n";
}
?>

This will break the iteration if that special character is found

$query = "SELECT * FROM Cities limit 5";
$result = mysqli_query($con, $query);
$isCharFound = false;

while(($row = mysqli_fetch_assoc($result)) && ($isCharFound === false))
{
    $city = $row['City'];
    if( strpos( $city, "ñ" ) !== false) {
        echo "City $city contains a ñ.  Skipping...\n\n";
        $isCharFound = true;
        continue;
    }
    echo "I should not see this if there is a ñ within $city \n\n";
}

If you want to only skip the record that has special character

$query = "SELECT * FROM Cities limit 5";
$result = mysqli_query($con, $query);

while($row = mysqli_fetch_assoc($result))
{
    $city = $row['City'];
    if( strpos( $city, "ñ" ) !== false) {
        echo "City $city contains a ñ.  Skipping...\n\n";
        continue;
    }
    echo "I should not see this if there is a ñ within $city \n\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM