搜索

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 "." 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性能调整May 14, 2025 am 12:13 AM

TheSecretTokeEpingAphp-PowerEdwebSiterUnningSmoothlyShyunderHeavyLoadInVolvOLVOLVOLDEVERSALKEYSTRATICES:1)emplactopCodeCachingWithOpcachingWithOpCacheToreCescriptexecution Time,2)使用atabasequercachingCachingCachingWithRedataBasEndataBaseLeSendataBaseLoad,3)

PHP中的依赖注入:初学者的代码示例PHP中的依赖注入:初学者的代码示例May 14, 2025 am 12:08 AM

你应该关心DependencyInjection(DI),因为它能让你的代码更清晰、更易维护。1)DI通过解耦类,使其更模块化,2)提高了测试的便捷性和代码的灵活性,3)使用DI容器可以管理复杂的依赖关系,但要注意性能影响和循环依赖问题,4)最佳实践是依赖于抽象接口,实现松散耦合。

PHP性能:是否可以优化应用程序?PHP性能:是否可以优化应用程序?May 14, 2025 am 12:04 AM

是的,优化papplicationispossibleandessential.1)empartcachingingcachingusedapcutorediucedsatabaseload.2)优化的atabaseswithexing,高效Quereteries,and ConconnectionPooling.3)EnhanceCodeWithBuilt-unctions,避免使用,避免使用ingglobalalairaiables,并避免使用

PHP性能优化:最终指南PHP性能优化:最终指南May 14, 2025 am 12:02 AM

theKeyStrategiestosiminificallyBoostphpapplicationPermenCeare:1)useOpCodeCachingLikeLikeLikeLikeLikeCacheToreDuceExecutiontime,2)优化AtabaseInteractionswithPreparedStateTemtStatementStatementSandProperIndexing,3)配置

PHP依赖注入容器:快速启动PHP依赖注入容器:快速启动May 13, 2025 am 12:11 AM

aphpdepentioncontiveContainerIsatoolThatManagesClassDeptions,增强codemodocultion,可验证性和Maintainability.itactsasaceCentralHubForeatingingIndections,因此reducingTightCightTightCoupOulplingIndeSingantInting。

PHP中的依赖注入与服务定位器PHP中的依赖注入与服务定位器May 13, 2025 am 12:10 AM

选择DependencyInjection(DI)用于大型应用,ServiceLocator适合小型项目或原型。1)DI通过构造函数注入依赖,提高代码的测试性和模块化。2)ServiceLocator通过中心注册获取服务,方便但可能导致代码耦合度增加。

PHP性能优化策略。PHP性能优化策略。May 13, 2025 am 12:06 AM

phpapplicationscanbeoptimizedForsPeedAndeffificeby:1)启用cacheInphp.ini,2)使用preparedStatatementSwithPdoforDatabasequesies,3)3)替换loopswitharray_filtaray_filteraray_maparray_mapfordataprocrocessing,4)conformentnginxasaseproxy,5)

PHP电子邮件验证:确保正确发送电子邮件PHP电子邮件验证:确保正确发送电子邮件May 13, 2025 am 12:06 AM

phpemailvalidation invoLvesthreesteps:1)格式化进行regulareXpressecthemailFormat; 2)dnsvalidationtoshethedomainhasavalidmxrecord; 3)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。