Intro:Repeating Actions with Loops

本文详细介绍了PHP中的循环结构,包括while循环、do-while循环及for循环,并通过具体示例展示了不同循环的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Like any good programming language,PHP also supports loops-essentially,the ability to repeat a series of actions until a prespecified condition is fulfilled.Loops are an important tool that help in automating repetitive tasks within a program.PHP supports four different types of loops,three of which are discussed in the following section(the fourth type is explained in the next chapter).

The while loop
The easiest type of loop to understand is the while loop,which repeats continuously while a prespecified condition is true.Here's an example, which uses a loop to repeatedly print an 'x' to the output page.
<?php
//repeat continuously until counter becomes 10
//output:'xxx...'
$counter=1;
while($counter<10){
  echo 'x';
  $conuter++;
}
//following lines
?>
-------------------------------------------------------------------------
Notice the condition enclosed in parentheses;so long as this condition evaluates to true,the code within the curly braces is executed.As soon as the condition becomes false,the loop stops repeating,and the lines following the loop are executed in the usual fashion.

The do-while loop
With a while loop,the condition to be evaluated is tested at the beginning of each loop iteration.There's also a variant of this loop,the do-while loop,which evaluates the condition at the end of each loop iteration.Here's a revision of the preceding example that illustrates it aaction:
<?php
//repeat continuously until counter becomes 10
//output:'xxxxxxxxx'
$counter=1;
do {
  echo 'x';
  $counter++;
}while($counter<10);
?>
The difference in structure should also be apparent:with a do-while loop,the condition to be evaluated now appears at the bottom of the loop block,rather than the beginning.

Note
The difference in behavior between a while loop and a do-while loop has one important implication:with a while loop,if the conditional expression evaluates to false on the pass itself,the loop will always be executed once,even if the conditional expression is false,because the condition is evaluated at the end of the loop iteration rather than the beginning.

The for loop
The while and do-while loopss are fairly simple:they repeat for so long as the specified condition remains true.But PHP also supports a more sophisticated type of loop,the for loop,which is useful when you need to execute a set of statements a specific number of times.
The best way to understand a for loop is by looking at some code.Here's a simple
example which lists the numbers between 1 and 10;

<?php
//repeat continuously until counter becomes 10
//output:
for($x=1;$x<10;$x++){
  echo "$x";
}
?>
In this listing,the loop begins by initializing the counter variable $x to 1;it then execute the statements that make up the loop.Once it reaches the end of the first loop iteration,it updates the loop counter by adding 1 to it,checks the conditional expression to ensure that the counter hasn't yet reached 10,and executes the loop once more.This process continues until the counter reaches 10 and the conditional expression becomes false.
As this listing illustrates,there are thus three expressions involved in the typical for loop,separeted by semicolons and enclosed in parentheses:
------------------
The first of these is an assignment expression,which initializes the loop counter to a specific value-in this case,assigning the valus 1 to the variable $x.
The second is a conditional expressio,which must evaluate to either true or false;the loop will continue to execute so long as this condition remains true.Once the condition becomes false,the loop will stop executing.
The third is again an assignment expression,which is executed at the end of each loop iteration,and which updates the loop counter with a new value-in this case,adding 1 to the valus of $x.

Here's another example,this one demonstraing how to use a for loop to generate an ordered HTML list:
<?php
//generate ordered list of 6 itmes
echo "<ol>";
  for($x=1;$x<7;$x++){
echo "<li>Iten $x<li>";
}
echo ""</ol>;
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值