PHP的break語句用於立即退出循環,而無需等待返回for循環、while循環、do-while、switch和for-each循環等條件語句。如果存在多個循環並且使用了break語句,則它僅從第一個內部循環退出。 Break 存在於語句區塊內,並為使用者提供了在需要時退出循環的完全自由。
廣告 該類別中的熱門課程 財務建模與估值 - 專業化 | 51 課程系列 | 30 次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法:
<?php //variable declarations //conditional statement block { break; } ?>
流程圖:
如上所示,當迴圈條件滿足時,程式碼會先進入條件語句區塊,並不斷執行迴圈中的語句,直到條件不滿足為止。當程式碼中寫入break語句時,一旦程式遇到它,無論條件是否滿足,程式碼都會退出當前循環,如圖所示。
讓我們透過為不同場景中的每個條件語句舉幾個範例並檢查其行為來了解break語句的工作原理。
「for」迴圈內的 Break 語句。
代碼:
<?php $var = 1; for ($var = 0;$var <= 10;$var++) { if ($var==5) { break; } echo $var; echo "\n"; } echo "For loop ends here" ; ?>
輸出:
這裡我們將 1 初始化為變數“var”,在 for 迴圈中印出從 1 到 10 的數字。 「var」開始列印從 1 開始的增量數字,直到遇到 if 循環條件。這裡我們提到,一旦變數的值達到 5,它就應該退出循環。這是使用如圖所示的break語句完成的。我們可以在輸出中看到相同的內容,因為一旦執行了break語句,即使不滿足for迴圈條件,它也會從for迴圈中印出「For迴圈結束於此」。因此,break 語句來自所有其他迭代的整個邏輯。
此範例是檢查 while 迴圈中的break語句的功能。
代碼:
<?php $var = 0; while( $var < 10) { $var++; if( $var == 5 )break; echo ("Current variable value = $var"); echo "\n"; } echo ("Exited loop at variable value = $var" ); ?>
輸出:
在上面的程式中,變數「var」首先被初始化為 0,然後使用 while 迴圈將其值增加 1 並列印相同的值。我們正在編寫一個 if 條件,其中一旦變數值等於 5,我們就使用 Break 語句來讓程式碼退出。此中斷使其從目前 while 迴圈退出,即使不滿足遞增變數直到值 10 的指定條件。遇見了。我們正在顯示循環中斷時的變數值。
這裡我們在 foreach 迴圈中實作break語句。
代碼:
<?php $array = array('A', 'B', 'C', 'D', 'E', 'F'); foreach ($array as $let) { if ($let == 'E') { break; } echo "$let \n"; }
輸出:
在這個程式中,我們先宣告一個包含字母集合的陣列。然後透過使用 foreach 循環,我們一一列印數組的所有元素。一旦數組指標的值達到字母“E”,請引入 if 條件語句來中斷迴圈。因此,在遇到break語句時,代碼退出而不列印數組中的下一個字母,即“F”。
break 最常見的應用是在 switch 語句中,如下圖所示。
代碼:
<?php $a=1; switch ($a) { case 0: echo "a equals 0"; break; case 1: echo "a equals 1"; break; case 2: echo "a equals 2"; break; } ?>
輸出:
這是一個簡單的 switch 語句的範例,我們先將變數值初始化為 1。然後透過使用 switch 條件,我們檢查變數值並在條件匹配時列印它。
這裡讓我們來看看當有兩個或多個迴圈(條件語句)時,break 語句的工作原理。
代碼:
<?php // PHP program to verify break of inner loop // Declaration of 2 arrays as below $array1 = array( 'One', 'Two', 'Three' ); $array2 = array( 'Three', 'One', 'Two', 'Four' ); // Outer foreach loop foreach ($array1 as $a1) { echo "$a1 "; // Inner nested foreach loop foreach ($array2 as $a2) { if ($a1 != $a2 ) echo "$a2 "; else break 2; } echo "\n"; } echo "\n Loop Terminated"; ?>
輸出:
Here we are using 2 nested foreach loops and also showing a case of using “break 2” which breaks out of both the loops in contrast to the “break” statement which breaks out of only the inner loop.
We have declared two arrays array1 and array2 and we are displaying the values of array2 for each value of array1 until and unless the value of array1 is not equal to array2. Once the value in array1 becomes the same as array2 we are breaking both loops by making use of break 2 statement which prevents the code from executing any more statements.
Here we shall see how to use break statements to come out of “n” number of loops (conditional statements).
Code:
<?php ## Outermost first for loop for($a=1;$a<5;$a++){ echo ("Value of a is = $a"); echo "\n"; ## Second for loop for($b=1;$b<3;$b++){ echo ("Value of b is = $b"); echo "\n"; ## Third for loop for($c=2;$c<3;$c++){ echo ("Value of c is = $c"); echo "\n"; ## Fourth for loop for($d=2;$d<4;$d++){ echo ("Value of d is = $d"); echo "\n"; if( $a == 3 ){ break 4; } } } } } echo 'Loop has terminated and value of a = '.$a; ?>
Output:
The break statement followed by the number of loops that need to be exited from are used to terminate from “n” number of loops.
Syntax:
break n;
where n is the number of loops that need to be exited from the loop.
We are using 4 nested for loops for this purpose. Variables a, b, c, d is initialized respectively for each for loop and we are incrementing their values by 1. The same values are displayed in the output to understand its flow better. At last, we are giving a condition for all the 4 loops to break if the value of the first variable becomes equal to 3 which it eventually did and can be shown in the output. We are printing the loop has terminated at the end along with as value to note the break’s functionality.
When we are using one or more conditional statements in our code and we need to exit from the same at a particular point, we can use the break statement. Basically, it helps to terminate from the code when the condition we give falls TRUE. We can also pass an integer along with break to terminate from any number of existing loops instead of declaring the break again and again.
以上是PHP 的破解的詳細內容。更多資訊請關注PHP中文網其他相關文章!