首頁  >  文章  >  後端開發  >  PHP 迴圈

PHP 迴圈

WBOY
WBOY原創
2024-08-29 12:41:00763瀏覽

PHP循環是一種程式碼,可以幫助我們在循環內部運行一些程式碼,根據我們的輸入要求一遍又一遍地運行,這些循環將幫助我們無限地運行程式碼並完成任務,如下所示我們希望在循環內一次又一次地執行相同的程式碼,直到條件變成假,否則程式碼會連續運作。這個字表示只有當某個條件為真時才會重複,這在迴圈參數中提到,用來檢查 PHP 迴圈的條件。

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

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

PHP 的不同循環

與其他程式語言一樣,PHP 提供了不同的循環概念。它們是:WHILE 循環、DO WHILE 循環、FOR 循環、FOREACH 循環。下面您將詳細了解 PHP 的每個迴圈概念。

PHP 迴圈

1. While 迴圈

只有當迴圈中提到的條件為真時,While 迴圈才會執行PHP 的while 迴圈括號內的特定/某些程式碼區塊;如果條件為false,則While 迴圈會中斷程式碼,這是在不斷運行程式碼的過程中。

文法:

While(Condition to check){
//Code which is need to executed or the code statements which is to run
}

說明:

在上面的語法中,在括號內提到了一個while 循環,只有當提到的條件為True 時才運行循環內的語句,否則循環內的程式碼將不會透過打破循環來運行跳出循環/ while 迴圈。

範例:

下面的範例由 while 循環編程組成,用於列印從 1 到 10 的數字列表。在下面的範例中,變數 1 被賦值為數字 1,然後循環程式在 $i 變數的幫助下開始值和 while 迴圈。 while迴圈以條件i

代碼:

<?php
$i = 1;
while($i <= 10){
echo " Now The number is " . $i . "<br>";
$i=$i+1;
}
?>

輸出:

PHP 迴圈

2.執行 While 迴圈

Do While 迴圈先執行迴圈內的程式碼,然後檢查迴圈條件,而 While 迴圈則在執行迴圈內的程式碼之前檢查循環條件。

文法:

do{
//Programming statements which is need to be executed only if the loop condition is true
}
While(condition to check);

範例:

下面的程序包含 2 個 do while 程序,用於列印 1-10 之間的偶數列表和 1-10 之間的奇數列表。程式還列印奇數、偶數的總和以及 1-10 之間所有自然數的總和。第一個 do-while 迴圈檢查變數 $i 的值,看看它是否可以被值「2」整除。如果為 True,則會列印該值,並且 $k 值將儲存 $i 值;否則,什麼也不會發生,只是 $i 變數值增加。

同樣,循環繼續,直到 $i 值達到值「10」。類似地,其他 do while 迴圈也透過檢查 $j 值是否不會被 2 個值除來運作。如果為 True,則會列印 $j 值,並且 $m 將儲存該值。最後,將偶數總和儲存在變數$k中,將奇數總和儲存在變數$l中。另外,將所有自然數的總和儲存在變數 $m 中。在輸出中顯示這些值,如圖所示。

代碼:

<?php
$i = 1;
echo "List of Even Numbers between 1-10:: ";
$k = 0;
$m = 0;
do{
if($i%2==0){
echo " $i " ." , ";
$k=$k+$i;
}
$m=$m+$i;
$i=$i+1;
}while($i <= 10);
echo "<br>"." Sum of the total even numbers between 1-10 ::"." $k";
echo "<br>";
$j = 1;
$l = 0;
echo "List of the ODD Numbers between 1-10:: ";
do{
if($j%2!=0){
echo " $j " ." , ";
$l=$l+$j;
}
$j=$j+1;
}while($j <= 10);
echo "<br>"." Sum of the total odd numbers between 1-10 ::"." $l";
echo "<br>";
echo "<br>"." Sum of the total natural numbers between 1-10 ::"." $m";
echo "<br>";
?>

輸出:

PHP 迴圈

3. For 循環

For 迴圈在比較 While 迴圈和 Do While 迴圈時有些不同。如果滿足特定條件,程式碼將重複執行。循環根據指定條件多次執行程式碼。

For 循環將有 3 個參數。它們是初始化、條件和For迴圈括號內的增量值。

文法:

for(initialization value; condition value; incrementing value){
//Programming code statements which is need to be executed when condition of the loop becomes TRUE
}

For 循環的參數:

  • Initialization: In For Loop, this is the value/variable value to start the program.
  • Condition: In For Loop, this is the value/variable value that must be checked. If the condition becomes true, then the program statements will run continuously by checking the condition.
  • Incrementing/Incrementation: The program will increment the initial or running value of the program statements by 1 or another value as required, depending on our needs, in a For loop.

Example:

The below for loop example will print the list of the natural numbers between 1-30 and the sum of all the values between 1-30.

To begin, we set $i as 1 for the initial value. The condition is that $i should be less than or equal to 30, with an increment of $i+1. For loop will print the $i value until the i value becomes 30, and the $j variable’s value will store all the numbers/values of the $i variable and then sum them one by one in the loop until the I value reaches 30. After printing all the natural numbers using the For Loop, the sum of all natural numbers between 1-30 will be displayed.

Code:

<?php
echo "List of the Natural Numbers between 1-30 :: ";
$j=0;
for($i=1; $i<=30; $i++){
echo "$i" . " , ";
$j=$j+$i;
}
echo "<br>";
echo "Sum of all the natural numbers between 1-30 :: ";
echo "$j";
echo "<br>";
?>

Output:

PHP 迴圈

4. Foreach Loop

PHP uses the “foreach” loop concept to iterate over arrays or multiple arrays.

Syntax:

foreach($array as $value){
//Programming code which is need to be executed
}

Example:

The below example will print the values from the $colors1 variable. $colors1 variable values are the list of the colors. Using the foreach loop concept will print the colors in the array individually.

Code:

<?php
$colors1 = array("Yellow", "Red", "Blue", "Green",);
// Colors array accessing in the loop
foreach($colors1 as $value1){
echo $value1 . "<br>";
}
?>

Output:

PHP 迴圈

以上是PHP 迴圈的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 的破解下一篇:PHP 的破解