PHP循环是一种代码,可以帮助我们在循环内部运行一些代码,根据我们的输入要求一遍又一遍地运行,这些循环将帮助我们无限地运行代码并完成任务,如下所示我们希望在循环内一次又一次地执行相同的代码,直到条件变为假,否则代码会连续运行。这个词表示只有当某个条件为真时才会重复,这在循环参数中提到,用于检查 PHP 循环的条件。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
与其他编程语言一样,PHP 提供了不同的循环概念。它们是:WHILE 循环、DO WHILE 循环、FOR 循环、FOREACH 循环。下面您将详细了解 PHP 的每个循环概念。
只有当循环中提到的条件为真时,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; } ?>
输出:
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>"; ?>
输出:
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 循环的参数:
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 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中文网其他相关文章!