繼續使用 PHP

WBOY
發布: 2024-08-28 17:46:57
原創
499 人瀏覽過

Continue 語句是在條件語句中使用的語句,指示程式碼跳過正在進行的迴圈的剩餘迭代,並在條件評估時繼續執行並跳到下一次執行的開始。在 PHP 中,最常見的地方是 switch 語句中使用 continue 語句。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

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 範例

讓我們透過以下幾個詳細範例來了解 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 的值初始化為零。然後,我們使用 for 迴圈將其值加一,直到其值達到 10。另一個條件語句 if 迴圈也用於在 a 的值達到等於 4 時中斷此迴圈。因此,它會跳出循環並然後列印“循環結束”,而不是列印值 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,但增量再次從 5 開始,直到 10,這是 for 循環條件,然後列印“End of for loop”,表明它已脫離 for a 循環。因此, 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 透過使用 modulo 2 函數來跳過偶數。我們在這裡使用 continue 語句,它會跳過目前迭代,因此這裡不會列印任何輸出。

在迴圈的第二部分中,我們使用變數 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中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!