Question: I have a coworker that I noticed was using his foreachs in the following fashion:

```
foreach ($var as $var) {
    // do stuff here
}

```

I have tested the code above and it works correctly to my surprise. Would the PHP gurus like to hop in and tell me why this is wrong? It feels very wrong.

Answer: 
Because it changes the value of `$var`. After the `foreach()` it's no longer an array but is set to the last value in the array.

```
$var = array('apple', 'orange');
foreach ($var as $var) {
    echo $var."<br/>";
}
echo $var; //orange

```

If you don't want to change the variable's value, it'll need to be a different variable name:

```
$var = array('apple', 'orange');
foreach ($var as $fruit) {
    echo $fruit."<br/>";
}
echo $var; //array

```

As @UselessIntern pointed out, it's fine if you're not going to use the variable after looping through it, but it's definitely not encouraged because it can lead to confusion.

As @PLB pointed out, it iterates over a copy of the `$var` not `$var` itself. So every iteration the value of `$var` is changing, but it doesn't break the loop because it's looping over the copy that was created.
Even it feels wrong, it still works because the moment the [*`foreach`*](http://php.net/foreach) starts, PHP internally has already access on the data.

So even `$var` gets overwritten, in memory this data is still present (the original array) and `$var` is set in each iteration to the current value of it.

The concrete problem you've spotted and about which you say is wrong is also known as *variable-reuse* which you should prevent because this is a [*code-smell*](http://c2.com/cgi/wiki?CodeSmell).

It not only feels wrong, it is wrong to write such code. Tell your co-worker so you can write better code together.