2 ms·
> That would be very strange behavior though that has no equivalence in any other part of the language. How is it strange? When you declare your iteration vari
by jessedhillon 15y ago
> That would be very strange behavior though that has no equivalence in any other part of the language.
How is it strange? When you declare your iteration variable, it unsets any existing definition of that variable if there is one, before it inserts the new variable. Why would anyone enter a loop and expect the iteration variable to have a value defined outside the loop?
Moreover, even if someone can find a reason why -- why make define the language around a need to support such badly designed programs.
> I'm afraid the behavior it has is the expected behavior.
Fine. s/expected/intuitive/g
> The problem isn't the language in this case.
Agreed. It's the people who designed it.
> These problems just aren't that bad.
This is my point: the individual issues are all workable. The real problem is that the language is maintained by people who have no idea how to design a language, frankly.
- wvenable 15y ago> How is it strange? When you declare your iteration variable You're not declaring anything; PHP doesn't have variable declarations. In this case, you're either creating a new variable or using an existing one -- just like everywhere else. But I'll concede the point. While the code is technically correct, unsetting the loop variable might be a nice addition. But I fully understand why they wouldn't want to make this change.
- rjh29 15y ago> Why would anyone enter a loop and expect the iteration variable to have a value defined outside the loop? Thus us the takeaway point, I think. The loop would work perfectly fine if $bar was lexically scoped to the foreach, but it isn't. That's a general weakness in PHP compared to pretty much any other scripting language used today.