Home  >  Article  >  Backend Development  >  Notes on using continue in PHP to skip the remaining code in this loop

Notes on using continue in PHP to skip the remaining code in this loop

陈政宽~
陈政宽~Original
2017-06-28 15:38:161633browse

continueThe jump statement is used to skip the statement that specifies the condition in this loop, and continue to execute other loop statements. The following article mainly introduces you to a point to note about using continue to skip the remaining code in this loop in PHP. The content in the article is relatively basic, and friends in need can refer to it. Let’s take a look together.

Preface

As we all know, in PHP, continue is used in the loop structure to skip the remaining code in this loop and The next iteration of the loop begins when the condition evaluates to true. It must be noted that when using continue, you must use ";" to separate other codes, otherwise it may cause errors!

continueUsage:


<?php
for ($n = 0; $n < 5; $n++) {
 if ($n == 2)
  continue;
  echo "$n\n";
}
?>


Output Result:

0 1 3 4

Obviously, when $n is equal to 2, the output is skipped, which is exactly what we want. If the semicolon is missing, an error will be reported!

Error code:


<?php
for ($n = 0; $n < 5; $n++) {
 if ($n == 2)
  continue
  echo "$n\n";
}
?>


Error message:

Parse error: syntax error, unexpected 'echo' (T_ECHO) in D:\phpStudy\WWW\demo\fun\continue.php on line 5

So note: When we use continue, we must be careful not to miss the semicolon!

Summarize

The above is the detailed content of Notes on using continue in PHP to skip the remaining code in this loop. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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