php循环套循环_PHP循环简介

php循环套循环

While循环 ( While Loops )

In PHP, there are several different types of loops. Basically, a loop evaluates a statement as true or false. If it is true, the loop executes some code and then alters the original statement and starts all over again by re-evaluating it. It continues to loop through the code like this until the statement becomes false.

在PHP中,有几种不同类型的循环。 基本上,循环将一条语句评估为true或false。 如果为真,则循环执行一些代码,然后更改原始语句,并通过重新评估它重新开始。 它继续像这样循环遍历代码,直到语句变为假。

Here is an example of a while loop in its simplest form:

这是while循环最简单形式的示例:

The code states that while a number is greater than or equal to 10, it prints the number. The ++ adds one to the number. This could also be phrased as $num = $num + 1. When the number becomes greater than 10 in this example, the loop stops executing the code within the brackets.

该代码指出,当一个数字大于或等于10时,它将打印该数字。 ++在数字上加一。 也可以用$ num = $ num + 1来表示 在此示例中,当数字大于10时,循环将停止执行方括号内的代码。

Here is an example of combining a loop with a conditional statement.

这是将循环与条件语句组合在一起的示例。

";
}
else
{
print $num . " is not less than 5
";
}
$num++;
}
?> 

对于循环 ( For Loops )

A for loop is similar to a while loop in that it continues to process a block of code until a statement becomes false. However, everything is defined in a single line. The basic structure for a for loop is:

for循环类似于while循环,因为它继续处理代码块,直到语句变为假为止。 但是,所有内容都在一行中定义。 for循环的基本结构是:

for ( start; conditional; increment) { code to execute; }

for(开始;条件;增量){要执行的代码; }

Let's go back to the first example using the while loop, where it printed out the numbers 1 through 10,  and do the same thing using a for loop.

让我们回到使用while循环的第一个示例,其中打印出数字1到10,并使用for循环执行相同的操作。

The for loop can also be used in conjunction with a conditional, just like we did with the while loop:

for循环也可以与条件结合使用,就像我们使用while循环一样:

 ";
}
else
{
print $num . " is not less than 5
";
}
}
?> 

Foreach循环 ( Foreach Loops )

To understand foreach loops you have to know about arrays. An array (unlike a variable) contains a group of data. When using a loop with an array, instead of having a counter that goes until proven false, the foreach loop continues until it has used all values in the array. So for example, if an array contained five pieces of data, then the foreach loop executes five times. 

要了解foreach循环,您必须了解数组 。 数组(与变量不同)包含一组数据。 当对数组使用循环时,foreach循环将继续使用直到数组使用了所有值,而不是使用直到证明为假的计数器为止。 因此,例如,如果一个数组包含五段数据,那么foreach循环将执行五次。

A foreach loop is phrased like this:

foreach循环的表达方式如下:

FOREACH (array as value) { what to do; }

FOREACH(数组作为值){做什么; }

Here is an example of a foreach loop:

这是一个foreach循环的示例:

When you understand this concept, you can use the foreach loop to do more practical things. Let's say an array contains the ages of five family members. A foreach loop can determine how much it costs for each of them to eat on a buffet that has varied prices based on age using the following pricing system: Under 5 is free, 5-12 years costs $4 and over 12 years is $6.

了解此概念后,可以使用foreach循环执行更多实际操作。 假设一个数组包含5个家庭成员的年龄。 使用以下定价系统,foreach循环可以确定每个人在价格随年龄而变化的自助餐上吃多少钱:5岁以下免费,5-12岁花费4美元,12岁以上花费6美元。

 ";
}
print "The total is: $" . $t;
?> 

翻译自: https://www.thoughtco.com/an-introduction-to-loops-in-php-2693960

php循环套循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值