php php中for循环和for每个循环有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2590792/
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
what are the difference between for loop & for each loop in php
提问by user285007
What are the differences between the forloop and the foreachloop in PHP?
PHP中的for循环和foreach循环有什么区别?
回答by Lotus Notes
Foreach is great for iterating through arrays that use keys and values.
Foreach 非常适合遍历使用键和值的数组。
For example, if I had an array called 'User':
例如,如果我有一个名为“User”的数组:
$User = array(
'name' => 'Bob',
'email' => '[email protected]',
'age' => 200
);
I could iterate through that very easily and still make use of the keys:
我可以非常轻松地遍历它并仍然使用这些键:
foreach ($User as $key => $value) {
echo $key.' is '.$value.'<br />';
}
This would print out:
这将打印出:
name is Bob
email is [email protected]
age is 200
With forloops, it's more difficult to retain the use of the keys.
对于for循环,保留密钥的使用更加困难。
When you're using object-oriented practice in PHP, you'll find that you'll be using foreachalmost entirely, with forloops only for numerical or list-based things. foreachalso prevents you from having to use count($array)to find the total number of elements in the array.
当您在 PHP 中使用面向对象的实践时,您会发现您将foreach几乎完全使用for循环,仅针对数字或基于列表的事物使用循环。foreach还可以防止您不得不使用count($array)查找数组中的元素总数。
回答by Peter Ajtai
foreachis specifically for iterating over elements of an array or object.
foreach专门用于迭代数组或对象的元素。
foris for doing something... anything... that has a defined start condition, stop condition, and iteration instructions.
for用于做某事……任何……具有定义的开始条件、停止条件和迭代指令的事情。
So, forcan be used for a much broader range of things. In fact, without the third expression - without the iteration instructions - a forbecomes a while.
因此,for可以用于更广泛的事物。事实上,没有第三个表达式 - 没有迭代指令 - afor变成了 a while。
Examples:
例子:
// Typical use of foreach
// It's strength is iterating over arrays & objects
$people = array("Tom", "Dick", "Hairy");
foreach ($people as $person) {
echo "$person <br/>"; }
Now you could do the exact same thing with for, but why bother? Instead forcan be used for completely different things:
现在你可以用 做同样的事情for,但何必呢?相反for可以用于完全不同的事情:
// Prints random names from array until Hairy is picked
for ($people = array("Tom", "Dick", "Hairy"); // initial condition
$people[0] != "Hairy"; // stop condition
shuffle($people) // iteration instructions
) {
echo "$people[0] <br/>";
}
The initial condition is done before the forloop once, no matter what. If the stop condition evaluates to falsethe loop will be immediately stopped. The change instructions are performed at the end of each loop. Notice that the change instructions don't have to be increments.
for无论如何,初始条件在循环之前完成一次。如果停止条件评估为false循环将立即停止。更改指令在每个循环结束时执行。请注意,更改指令不必是增量。
Here is an example of turning a forloop into a whileloop by leaving out the iteration instructions.
这是通过省略迭代指令将for循环变成while循环的示例。
// Does the loop a random number of times.
// No thired expression
for ($rand = function() {$array = array(true, true, true, true, false);
shuffle($array);
return $array;
};
current($rand());
// empty third expression
) {
echo "I bring nothing to the table.<br/>";
}
回答by gbarry
A "for" loop gives you an incrementing number (in its most common use) which you can use any way you like.
“for”循环为您提供一个递增的数字(在其最常见的用途中),您可以使用任何您喜欢的方式。
"foreach" is a special construct made for looking at successive members of an array.
“foreach”是一个特殊的构造,用于查看数组的连续成员。
As an example, you can use a "for" loop to create something that does what "foreach" does. But foreach does that with less required code.
例如,您可以使用“for”循环来创建执行“foreach”功能的内容。但是 foreach 用较少的代码就可以做到这一点。
回答by alex
It should be pretty simple
应该很简单
foreachabstracts away some of the complexity and is usually easier. I use this whenever I don't need to know the numerical index of the array or $key => $valuewon't provide me with it.
foreach抽象了一些复杂性,通常更容易。每当我不需要知道数组的数字索引或$key => $value不向我提供它时,我就会使用它。
foris the older C style where you must first perform a count()so you know how many iterations the loop requires. It is useful when you need to know the index, or to count backwards or step through in different groups.
for是旧的 C 风格,您必须首先执行 acount()以便您知道循环需要多少次迭代。当您需要知道索引,或者在不同的组中向后计数或步进时,它很有用。
回答by Chris
回答by Your Common Sense
foreachbeing used to iterate arrays and nothing else.foris the general purpose counter-based loop
foreach用于迭代数组而不是其他任何东西。for是通用的基于计数器的循环
回答by Your Common Sense
Better and easy answer is: Difference between Foreach and For Loop:-
更好更简单的答案是:Foreach 和 For 循环之间的区别:-
1. Foreach Loop:- Details are following.
a) Foreach loop used when you have an array, without array it's not worked.
b) Loop working at the end of array count. For example an array have 5 value
then loop run 5 times.
c) Syntax is following.
$array = array("Surinder","Rahul","Manoj","Bharti","Rana","Manish");
Foreach($array as $name ){
echo "Employe Name is ".$name.".";
}
This will print as following.
Employe Name is Surinder.
Employe Name is Rahul.
Employe Name is Manoj.
Employe Name is Bharti.
Employe Name is Rana.
Employe Name is Manish.
2. For Loop:- Details are following.
a) For loop used according to condition.
b) Loop working at the end of given condition.
c) Syntax is following.
$array = array("Surinder","Rahul","Manoj","Bharti","Rana","Manish");
For($i=0;$i<6;$i++){
echo "Employe Name is ".$array[$i].";
}
At the place of 6,You can used count array function.
This will print as following.
Employe Name is Surinder.
Employe Name is Rahul.
Employe Name is Manoj.
Employe Name is Bharti.
Employe Name is Rana.
Employe Name is Manish.
These are the difference between Foreach and for loop.
For more info go there:http://ibmphp.blogspot.com/2012/10/difference-between-foreach-and-for-loop.html
有关更多信息,请访问:http: //ibmphp.blogspot.com/2012/10/difference-between-foreach-and-for-loop.html
回答by Rahul Khandelwal Systematix
forloop is used if we know already that how many times the script should be run but in the case of foreachloop, we don't have any idea about the number of iteration.
如果我们已经知道脚本应该运行多少次,则使用for循环,但在foreach循环的情况下,我们不知道迭代次数。
Also foreachloop is used to iterate only arraysand objects.
此外,foreach循环仅用于迭代数组和对象。
You can refer the link for better understanding the difference between forand foreachloop -
您可以参考链接以更好地理解for和foreach循环之间的区别-
https://www.quora.com/What-is-the-difference-between-for-and-foreach-in-php
https://www.quora.com/What-is-the-difference-between-for-and-foreach-in-php

