What are the differences between the for
loop and the foreach
loop in PHP?
foreach
being used to iterate arrays and nothing else.
for
is the general purpose counter-based loop
Foreach is basically a shortcut for doing the following
//Foreach method
foreach ($myArray as $myVar)
{
}
//Normal for equivalent
for ($i = 0; $i < $limit; $i++)
{
$myVar = $myArray[$i];
}
But there are other issues too, read this article about it
Better and easy answer is: Difference between Foreach and For Loop:-
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
for loop is used if we know already that how many times the script should be run but in the case of foreach loop, we don't have any idea about the number of iteration.
Also foreach loop is used to iterate only arrays and objects.
You can refer the link for better understanding the difference between for and foreach loop -
https://www.quora.com/What-is-the-difference-between-for-and-foreach-in-php
foreach
is specifically for iterating over elements of an array or object.
for
is for doing something... anything... that has a defined start condition, stop condition, and iteration instructions.
So, for
can be used for a much broader range of things. In fact, without the third expression - without the iteration instructions - a for
becomes 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/>"; }
Working example
Now you could do the exact same thing with for
, but why bother? Instead for
can be used for completely different things:
// 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/>";
}
Working example
The initial condition is done before the for
loop once, no matter what. If the stop condition evaluates to false
the 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.
Here is an example of turning a for
loop into a while
loop by leaving out the iteration instructions.
// 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/>";
}
Working example
Foreach is great for iterating through arrays that use keys and values.
For example, if I had an array called 'User':
$User = array(
'name' => 'Bob',
'email' => 'bob@example.com',
'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 bob@example.com
age is 200
With for
loops, it's more difficult to retain the use of the keys.
When you're using object-oriented practice in PHP, you'll find that you'll be using foreach
almost entirely, with for
loops only for numerical or list-based things. foreach
also prevents you from having to use count($array)
to find the total number of elements in the array.