PHP で続行する

WBOY
リリース: 2024-08-28 17:46:57
オリジナル
499 人が閲覧しました

Continue ステートメントは、条件ステートメント内で使用され、進行中のループの残りの反復をスキップし、条件評価時に実行を継続して次の実行の開始にジャンプするようにコードに指示します。 PHP では、Continue ステートメントが最も一般的に使用される場所は switch ステートメントです。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Continue は、実行がスキップして最後まで進む内部ループのレベルを定義するオプションの数値パラメーターを受け取ります。これは、値が 1 の場合、現在のループの最後までスキップすることを意味します。 Break ステートメントはループを完全に終了するために使用されますが、Continue は現在のループから次のループに進むショートカットとして使用されます。どちらもループ上のコーダーに追加の制御を提供し、必要に応じて操作できるようにします。

構文:

while ( condition 1)
{
//declaration statements
if ( condition 2 )
{
//PHP code goes here
continue ;
}
// Operational Statements
}
ログイン後にコピー

上で見たように、最初に while ループをその条件とともに宣言しています。条件が満たされるとコードはループに入り、条件が真でなくなるまでループが続きます。コードは、条件が true の場合にのみ if ステートメントのループに入ります。ここでの continue ステートメントはこの繰り返しをスキップし、Continue 後のコードの後半部分は実行されません。したがって、制御は条件 1 を持つ while ループの次の反復に転送されます。

フローチャート

以下は、PHP での continue のフローチャートです。

PHP で続行する

フローチャートに見られるように、ループが実行されるコード内のステートメント ブロックには continue が常に存在します。 continue の条件が true の場合、後続のアクションをスキップし、次の反復に進みます。

PHP での続行の例

以下に示すいくつかの詳細な例を使用して、Continue ステートメントの正確な動作を見てみましょう。

例 #1

コード:

<?php
$a = 0;
for ($a = 0;$a <= 10;$a++)
{
if ($a==4)
{
break;
}
echo $a;
echo "\n";
}
echo "End of for loop" ;
?>
ログイン後にコピー

出力:

PHP で続行する

このプログラムでは、まず変数 a の値を 0 に初期化します。次に、for ループを使用して、その値が 10 に達するまでその値を 1 ずつ増分します。 a の値が 4 に達すると、このループを中断するために別の条件ステートメント if ループも使用されます。 したがって、ループから抜け出し、次に、値 5 を出力する代わりに、「ループの終わり」を出力します。理解しやすくするために、出力に増分された各値を出力しています。ご覧のとおり、break ステートメントにより出力は 4 で停止します。

同じコードに対して continue ステートメントが何を行うかを見てみましょう:

コード:

<?php
$a = 0;
for ($a = 0;$a <= 10;$a++)
{
if ($a==4)
{
continue;
}
echo $a;
echo "\n";
}
echo "End of for loop" ;
?>
ログイン後にコピー

出力:

PHP で続行する

上記の出力からわかるように、プログラムは前の出力と同様に 0 から 4 までの数字を出力しました。 if 条件ステートメントが TRUE の場合、 continue ステートメントがあるため、4 の出力はスキップされますが、ループは再度継続されます。したがって、出力には 4 は表示されませんが、for ループ条件である 5 から 10 までインクリメントが再び開始され、for a ループから抜け出したことを示す「for ループの終了」が出力されます。したがって、 continue は通常、その特定のインスタンスからスキップし、次のインスタンスからループを継続するために使用されます。

例 #2

コード:

<?php
$a=0;
foreach ($a as $k => $val) {
if (!($k % 2)) { // skip even members
continue;
}
do_something_odd($val);
}
$j = 0;
while ($j++ < 5) {
echo "Outermost while loop\n";
while (1) {
echo "Middle while loop\n";
while (1) {
echo "Innermost while loop\n";
continue 3;
}
echo "This text will never get printed\n";
}
echo "This text also will never get printed\n";
}
?>
ログイン後にコピー

出力:

PHP で続行する

上記のコードでは、if 条件ループで変数 k を使用し、モジュロ 2 関数を使用して偶数をスキップしています。ここでは現在の反復をスキップする continue ステートメントを使用しているため、ここでは出力は出力されません。

ループの 2 番目の部分では、変数 j を使用し、最初にそれをゼロに初期化します。次に、3 つの while ループ (最も外側のループ、中央のループ、最も内側のループ) を使用します。最も外側のループには、変数 j が 5 未満で、j が 1 ずつ増加し続けるときに実行される条件があります。中央と内側のループは無限であり、条件なしで実行し続けることを意味します。

Just like the break statement, we can also pass numeric parameters along with the continue statement. As shown in the above example, we are passing the number 3 which means that it skips 3 iterations of the loop. Hence the text which will never get printed is shown in the code. The same code when we give continue 2 will skip the innermost and the middle while loop and prints the last text. Similarly, continue 1 will skip only the innermost loop. However, note that continues 0 is not a valid statement to execute even though it was valid before as it was considered to be the same as continue 1.

Example #3

Continue can also be used in a switch statement as follows:

Code:

<?php
echo"\n";
echo"\n";
for ( $a = 0; $a < 5; $a++ ) {
switch ($a)
{
case 0:
echo $a . "\nb\n";
continue 2;
echo $a . "\na\n";
case 1:
echo $a . "\nb\n";
continue 2;
echo $a . "\na\n";
case 2:
echo $a . "\nb\n";
break;
echo $a . "\na\n";
}
echo "end\n";
}
echo"\n";
echo"\n";
?>
ログイン後にコピー

Output:

PHP で続行する

It can be seen that in a switch statement, break and continue statements work in the same way. But in the loops, they are used to stop the loop iteration and move on to the next. By using continue 2 we can come out of the switch inside a loop and continue to the next outer loop iteration.

Importance of Continue in PHP

  1.  The main purpose of the continue statement is inside loops for skipping a particular instance of iteration where the coder wants it.
  2. In case of a do-while loop, the continue statement takes control of the conditional statement part of the while loop.
  3. Even in a while loop, the continue again takes control of the condition of while loop.
  4. In for loop, the increment or decrement actions get executed after the continue statement.

Disadvantages of Continue in PHP

  1. We cannot use continue in a file name which is inside a loop as it will cause an error.
  2. Using continue makes the code hard to follow and does not look elegant.

Conclusion – Continue in PHP

Continue statements are majorly used in all kinds of loops or conditional statements in PHP. They basically stop the ongoing iteration of that loop but does not terminate the same. Continue statement present inside the statement block just hops on to the next iteration which succeeds in the conditional statement.

以上がPHP で続行するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!