PHP continue Statement | How to write a continue statement in PHP?

Summary: In this article, you will learn PHP continue Statement and How to write a continue statement in PHP? let’s understand in details with example.

Definiation and Usages

Sometimes a scenario arises wherever we wish to require to take control of the beginning of the loop, The continue statement is used to continue the loop. It continues this flow of the program and skips the remaining code at the desired condition. The continue statement is used within the looping and switch control structure when you directly jump to successive iteration.
The continue statement can be used with all kinds of loops like – for, while, do-while, and foreach loop. The continue statement permits the user to skip the execution of the code for the desired condition or specified conditions
It allows us to do this. once the keyword continues executed inside a loop the control automatically passes to the start of the loop. Continue is usually associated with the if.

Syntax:

jump-statement;
continue;

Flowchart:

PHP continue Statement

1. PHP continue Example in while loop

In the following example, here, we will print the even numbers between 1 to 40.

Even numbers between 1 to 40:
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40

2. PHP Continue Example with for loop

In the following example, here, we will print only those values of a and b that are same and skip others.

11
22
33
44
55

3. PHP continue Example with array of string

The given below following example prints the value of array elements except those for which the desired condition is true and the continue statement is used.

Eleven
Twelve
Thirteen
Fourteen
Fifteen

4. PHP continue Example with optional argument

The continue statement in PHP accepts an optional numeric value, that used accordingly. The numeric value describes how many nested structures it’ll exit. let’s Look at the below example to understand it a better way:

12
13
14
15
21
23
24
25
31
32
34
35
41
42
43
45
51
52
53
54

Question: To Print the List of odd numbers between 1 to 20

Answer: In this example, we will print the list of odd numbers between 1 to 20. In the while loop, we test the remainder (here $x%2) of every number, if the remainder is 0 then it becomes an even number, and to avoid printing of even numbers continue statement is immediately used and then control passes to the beginning of the loop.

List of odd numbers between 1 to 20
1
3
5
7
9
11
13
15
17
19

Conclusion:

In this article, you have learned continue Statement and also learned how to write a continue statement in PHP? I hope you will enjoy it!. if have any query then contact on info@tutorialscan.com I will try to resolve the problem.
Stay healthy, stay safe! Happy coding! enjoy it!