The role and use of the continue keyword in PHP

PHPz
Release: 2023-06-28 20:14:01
Original
2685 people have browsed it

The role and usage of the continue keyword in PHP

In PHP programming, continue is a very useful keyword. It is used to control the execution flow of loop statements, allowing to skip the remaining code in the current loop and directly enter the execution of the next loop.

The function of continue is to skip the code in the current iteration in the loop statement and start the next iteration directly. When the continue statement is executed, loop control will immediately go to the beginning of the loop body without executing the code after the continue statement.

Normally, the continue keyword is used in for, foreach, while and do-while loops. The following explains how to use continue in each loop.

  1. Using continue in a for loop

In a for loop, the continue statement is used to skip the current iteration and start the next iteration. For example:

for ($i = 1; $i <= 10; $i++) {
    if ($i % 2 == 0) {
        continue; // 跳过偶数的迭代
    }
    echo $i . " ";
}

// 输出结果:1 3 5 7 9
Copy after login

In the above example, when $i is equal to 2, 4, 6, 8 and 10, the continue statement will skip these even iterations and continue with the next iteration. Therefore, only odd numbers are output.

  1. Using continue in a foreach loop

In a foreach loop, the continue statement is also used to skip the current iteration and start the next iteration. For example:

$fruits = array("apple", "banana", "cherry", "date", "elderberry");

foreach ($fruits as $fruit) {
    if ($fruit == "cherry") {
        continue; // 跳过"cherry"的迭代
    }
    echo $fruit . " ";
}

// 输出结果:apple banana date elderberry
Copy after login

In the above example, when the value of the element is equal to "cherry", the continue statement will skip the iteration and only output the values ​​of other fruits.

  1. Using continue in a while loop

In a while loop, the continue statement is also used to skip the current iteration and start the next iteration. For example:

$i = 0;

while ($i < 5) {
    $i++;
    if ($i == 3) {
        continue; // 跳过$i等于3的迭代
    }
    echo $i . " ";
}

// 输出结果:1 2 4 5
Copy after login

In the above example, when $i is equal to 3, the continue statement will skip this iteration and only output other values.

  1. Using continue in a do-while loop

In a do-while loop, the continue statement is also used to skip the current iteration and start the next iteration. For example:

$i = 5;

do {
    $i--;
    if ($i == 3) {
        continue; // 跳过$i等于3的迭代
    }
    echo $i . " ";
} while ($i > 0);

// 输出结果:4 2 1 0
Copy after login

In the above example, when $i is equal to 3, the continue statement will skip this iteration and only output other values.

To summarize, the function of the continue keyword in a loop statement is to skip the current iteration and start the next iteration. By using continue, we can flexibly control the execution flow of the code in the loop to implement specific logic.

It should be noted that the continue statement can only be used within the loop body, otherwise it will cause a syntax error. In addition, if multiple nested loop statements are used, the continue statement will only skip the current iteration of the inner loop and will not affect the execution of the outer loop.

I hope this article will help you understand and use the continue keyword in PHP!

The above is the detailed content of The role and use of the continue keyword in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!