Jump to: navigation, search

Php continue

From w3cyberlearnings

Contents

continue

continue uses in the looping structures to skip the rest of the condition, and continue to execute at the next iteration.


Syntax

foreach($arr as $aVal) {
      statement
      continue;
}

while(expression) {
  if(express1==expression2) {
       continue;
  }
}

while (express) {
    switch (expres) {
    case condition1:
        statement
        continue;
    case condition2:
        statement2;
        continue;
    default:
        continue;
    }
}

Example 1: while loop without continue

<?php

$i = 25;
while ($i-- > 10) {
	
	echo $i . '<br/>';
}
?>

Output

24
23
22
21
20
19
18
17
16
15
14
13
12
11
10

Example 2: While loop with continue

  • Skip any condition when $i % 2 equal 0.
<?php

$i = 25;
while ($i-- > 10) {
	if ($i % 2 == 0) {
		continue;
	}
	echo $i . '<br/>';
}
?>


Output

23
21
19
17
15
13
11

Example 3: use the continue, but do the opposite from Example 2

<?php

$i = 25;
while ($i-- > 10) {
	if ($i % 2 != 0) {
		continue;
	}
	echo $i . '<br/>';
}
?>

Output

24
22
20
18
16
14
12
10

Related Links


Navigation
Web
SQL
MISC
References