php If 语句和 While 循环之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14606466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between an If statement and While loop
提问by Ben Beri
I read this Manual by PHP.com about While loops.
我阅读了 PHP.com 的有关While 循环的手册。
I don't understand the purpose of While loops in PHP.
我不明白 PHP 中 While 循环的目的。
It looks exactly like an ifstatement to me.
if对我来说,它看起来完全像一个声明。
What is the difference between an if statement and a while loop?
if 语句和 while 循环有什么区别?
How do while loops work, what do they do, and when should I use them?
while 循环是如何工作的,它们有什么作用,我应该什么时候使用它们?
For example, can't this:
例如,不能这样:
$i = 1;
while ($i <= 10) {
echo $i++;
}
be done like this?:
这样做?:
$i = 1;
if ($i <= 10) {
echo $i++;
}
回答by dsgriffin
An ifstatement checks if an expression is true or false, and then runs the code inside the statement only if it is true. The code inside the loop is only run once...
一个if语句检查,如果一个表达式是真还是假,然后运行,只有当它是真实的语句中的代码。循环内的代码只运行一次......
if (x > y)
{
// this will only happen once
}
A whilestatement is a loop. Basically, it continues to executethe code in the while statement for however long the expression is true.
一个while语句是一个循环。基本上,无论表达式为真多久,它都会继续执行while 语句中的代码。
while (x > y)
{
// this will keep happening until the condition is false.
}
When to use a while loop:
何时使用 while 循环:
While loops are best used when you don't know exactly how many times you may have to loop through a condition - if you know exactly how many times you want to test a condition (e.g. 10), then you'd use a for loop instead.
当您不确切知道可能需要循环遍历条件的次数时,最好使用 while 循环 - 如果您确切知道要测试条件的次数(例如 10),则可以使用 for 循环反而。
回答by Tom Smilack
A while loop will run as many times as it needs to while a condition is true, i.e., until that condition is false.
当条件为真时,while 循环将根据需要运行多次,即,直到该条件为假为止。
An if statement will execute onceif a condition is true.
如果条件为真,if 语句将执行一次。
A great way to understand concepts like this when you're just learning a language is to try them out:
当您刚学习一门语言时,理解此类概念的一个好方法是尝试它们:
<?php
$i = 1;
while ($i <= 10) {
echo $i++;
}
echo "\n";
$i = 1;
if ($i <= 10) {
echo $i++;
}
This results in:
这导致:
12345678910 1
回答by saurav jha
if command is only run in one condition in one time and its execute in only one statement in one time
if 命令一次只在一种条件下运行,并且一次只在一个语句中执行
while loop is manly use in infinite time for looping a statement while is executed in many statement in one time
while 循环在无限时间内用于循环语句 while 一次在多个语句中执行
回答by Brahm Dorst
Here's an example:
下面是一个例子:
Suppose you want a script that loops through an array and make one beep sound for each element of the array.
假设您想要一个循环遍历数组并为数组的每个元素发出哔声的脚本。
A WHILE loop would generate no beeps for an empty array.
WHILE 循环不会为空数组生成蜂鸣声。
An FOR loop will always run at least once, so an empty array would generate one beep.
FOR 循环将始终至少运行一次,因此空数组将产生一声哔哔声。

