continue

continue is used within looping structures to
skip the rest of the current loop iteration and continue execution
at the condition evaluation and then the beginning of the next iteration.

Note:
In PHP the
switch statement is
considered a looping structure for the purposes of
continue. continue behaves like
break (when no arguments are passed). If a
switch is inside a loop,
continue 2 will continue with the next iteration
of the outer loop.

continue accepts an optional numeric argument
which tells it how many levels of enclosing loops it should skip
to the end of. The default value is 1, thus skipping
to the end of the current loop.

because the entire continue print "$i\n"; is evaluated
as a single expression, and so print is called only
when $i == 2 is true. (The return value of
print is passed to continue as the
numeric argument.)

Note:

As of PHP 5.4.0, the above example will raise an
E_COMPILE_ERROR error.

Changelog for continue

Version

Description

5.4.0

continue 0; is no longer valid. In previous versions it was interpreted
the same as continue 1;.

User Contributed Notes 18 notes

The remark "in PHP the switch statement is considered a looping structure for the purposes of continue" near the top of this page threw me off, so I experimented a little using the following code to figure out what the exact semantics of continue inside a switch is:

and observed the different results. This made me come up with the following one-liner that describes the difference between break and continue:

continue resumes execution just before the closing curly bracket ( } ), and break resumes execution just after the closing curly bracket.

Corollary: since a switch is not (really) a looping structure, resuming execution just before a switch's closing curly bracket has the same effect as using a break statement. In the case of (for, while, do-while) loops, resuming execution just prior their closing curly brackets means that a new iteration is started --which is of course very unlike the behavior of a break statement.

In the one-liner above I ignored the existence of parameters to break/continue, but the one-liner is also valid when parameters are supplied.

In the same way that one can append a number to the end of a break statement to indicate the "loop" level upon which one wishes to 'break' , one can append a number to the end of a 'continue' statement to acheive the same goal. Here's a quick example:

a possible explanation for the behavior of continue in included scripts mentioned by greg and dedlfix above may be the following line of the "return" documentation: "If the current script file was include()ed or require()ed, then control is passed back to the calling file." The example of greg produces an error since page2.php does not contain any loop-operations.

So the only way to give the control back to the loop-operation in page1.php would be a return.

It goes to show that in a switch statement break and continue are the same. But in loops break stops the loop completely and continue just stops executing the current iterations code and moves onto the next loop iteration.

(only) the reason that is given on the "Continue with missing semikolon" example is wrong.

the script will output "2" because the missing semikolon causes that the "print"-call is executed only if the "if" statement is true. It has nothing to to with "what" the "print"-call would return or not return, but the returning value can cause to skip to the end of higher level Loops if any call is used that will return a bigger number than 1.

<?phpcontinue print "$i\n";?>

because of the optional argument, the script will not run into a "unexpected T_PRINT" error. It will not run into an error, too, if the call after continue does return anything but a number.

i suggest to change it from:because the return value of the print() call is int(1), and it will look like the optional numeric argument mentioned above.

tobecause the print() call will look like the optional numeric argument mentioned above.