If I declare a variable inside a foreach loop, such as:
foreach($myArray as $myData) {
$myVariable = \'x\';
}
Does PHP destroy it, and
The problem is $myVariable is not truly local to foreach only. So it can clobber a global variable under the same name.
A way around that is make your foreach an inline anonymous function.
E.g.
$myforeach=function(&$myArray){ // pass by ref only if modifying it
foreach($myArray as $myData) {
$myVariable = 'x';
}
};
$myforeach($myArray); // execute anonymous.
This way you guarantee it will not step on other globals.